-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.go
237 lines (204 loc) · 8.53 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
/*
Copyright (C) 2019 Ulbora Labs LLC. (www.ulboralabs.com)
All rights reserved.
Copyright (C) 2019 Ken Williamson
All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"flag"
"fmt"
"html/template"
"net/http"
"os"
"strconv"
//"encoding/json"
hd "github.com/Ulbora/GoAuth2/handlers"
m "github.com/Ulbora/GoAuth2/managers"
lg "github.com/Ulbora/Level_Logger"
db "github.com/Ulbora/dbinterface"
mdb "github.com/Ulbora/dbinterface_mysql"
"github.com/gorilla/mux"
)
//GO111MODULE=on go mod init github.com/Ulbora/GoAuth2
func main() {
mock := flag.Bool("mock", false, "use mock backend")
compressJwt := flag.Bool("compressJwt", false, "compress Jwt access token")
assets := flag.String("assets", "", "Assets to control")
flag.Parse()
var logger lg.Logger
logger.LogLevel = lg.InfoLevel
logger.Info("mock: ", *mock)
logger.Info("assets: ", *assets)
logger.Info("compressJwt: ", *compressJwt)
// logger.LogLevel = lg.OffLevel
var dbi db.Database
var mydb mdb.MyDB
var goauth2Host string
var goauth2User string
var goauth2Password string
var goauth2Database string
var authURL string
var tokenParams m.TokenParams
if os.Getenv("GOAUTH2_HOST") != "" {
goauth2Host = os.Getenv("GOAUTH2_HOST")
} else {
goauth2Host = "localhost:3306"
}
if os.Getenv("GOAUTH2_USER") != "" {
goauth2User = os.Getenv("GOAUTH2_USER")
} else {
goauth2User = "admin"
}
if os.Getenv("GOAUTH2_PASSWORD") != "" {
goauth2Password = os.Getenv("GOAUTH2_PASSWORD")
} else {
goauth2Password = "admin"
}
if os.Getenv("GOAUTH2_DATABASE") != "" {
goauth2Database = os.Getenv("GOAUTH2_DATABASE")
} else {
goauth2Database = "go_auth2"
}
if os.Getenv("AUTHENTICATION_SERVICE") != "" {
authURL = os.Getenv("AUTHENTICATION_SERVICE")
} else {
authURL = "http://localhost:3001/rs/user/login"
}
if os.Getenv("ACCESS_TOKEN_KEY") != "" {
tokenParams.AccessTokenKey = os.Getenv("ACCESS_TOKEN_KEY")
}
if os.Getenv("REFRESH_TOKEN_KEY") != "" {
tokenParams.RefreshTokenKey = os.Getenv("REFRESH_TOKEN_KEY")
}
if os.Getenv("TOKEN_ISSUER") != "" {
tokenParams.Issuer = os.Getenv("TOKEN_ISSUER")
}
if os.Getenv("TOKEN_AUDIENCE") != "" {
tokenParams.Audience = os.Getenv("TOKEN_AUDIENCE")
}
logger.Info("tokenParams: ", tokenParams)
logger.LogLevel = lg.OffLevel
mydb.Host = goauth2Host
mydb.User = goauth2User
mydb.Password = goauth2Password
mydb.Database = goauth2Database
dbi = &mydb
dbi.Connect()
var wh hd.WebHandler
var rh hd.RestHandler
var owh *hd.OauthWebHandler
if *mock {
owh = hd.UseMockWeb()
wh = owh.GetNewWebHandler()
orh := hd.UseMockRest()
rh = orh.GetNewRestHandler()
} else {
owh = hd.UseWebHandler(dbi, *compressJwt, authURL, &logger, &tokenParams)
wh = owh.GetNewWebHandler()
orh := hd.UseRestHandler(dbi, *assets, *compressJwt, authURL, &logger, &tokenParams)
rh = orh.GetNewRestHandler()
}
owh.Templates = template.Must(template.ParseFiles("./static/head.html", "./static/index.html",
"./static/login.html", "./static/authorizeApp.html", "./static/oauthError.html"))
router := mux.NewRouter()
port := "3000"
envPort := os.Getenv("PORT")
if envPort != "" {
portInt, _ := strconv.Atoi(envPort)
if portInt != 0 {
port = envPort
}
}
//web routes-------------------------------------
router.HandleFunc("/", owh.Index).Methods("GET")
router.HandleFunc("/login", wh.Login).Methods("GET")
router.HandleFunc("/login", wh.LoginUser).Methods("POST")
router.HandleFunc("/oauth/authorize", wh.Authorize).Methods("GET")
router.HandleFunc("/authorizeApp", wh.AuthorizeApp).Methods("GET")
router.HandleFunc("/applicationAuthorize", wh.ApplicationAuthorizationByUser).Methods("GET")
router.HandleFunc("/oauth/token", wh.Token).Methods("POST")
router.HandleFunc("/oauthError", wh.OauthError).Methods("GET")
//REST routes--------------------------------------
//Client
router.HandleFunc("/rs/client/add", rh.AddClient).Methods("POST")
router.HandleFunc("/rs/client/update", rh.UpdateClient).Methods("PUT")
router.HandleFunc("/rs/client/get/{id}", rh.GetClient).Methods("GET")
router.HandleFunc("/rs/client/admin/get", rh.GetClientAdmin).Methods("GET")
router.HandleFunc("/rs/client/list", rh.GetClientList).Methods("GET")
router.HandleFunc("/rs/client/search", rh.GetClientSearchList).Methods("POST")
router.HandleFunc("/rs/client/delete/{id}", rh.DeleteClient).Methods("DELETE")
//clientGrantType
router.HandleFunc("/rs/clientGrantType/add", rh.AddGrantType).Methods("POST")
router.HandleFunc("/rs/clientGrantType/list/{clientId}", rh.GetGrantTypeList).Methods("GET")
router.HandleFunc("/rs/clientGrantType/delete/{id}", rh.DeleteGrantType).Methods("DELETE")
//clientAllowedUri
router.HandleFunc("/rs/clientAllowedUriSuper/add", rh.AddAllowedURISuper).Methods("POST")
router.HandleFunc("/rs/clientAllowedUri/add", rh.AddAllowedURI).Methods("POST")
router.HandleFunc("/rs/clientAllowedUriSuper/update", rh.UpdateAllowedURISuper).Methods("PUT")
router.HandleFunc("/rs/clientAllowedUri/update", rh.UpdateAllowedURI).Methods("PUT")
router.HandleFunc("/rs/clientAllowedUri/get/{id}", rh.GetAllowedURI).Methods("GET")
router.HandleFunc("/rs/clientAllowedUri/list/{clientId}", rh.GetAllowedURIList).Methods("GET")
router.HandleFunc("/rs/clientAllowedUri/delete/{id}", rh.DeleteAllowedURI).Methods("DELETE")
//clientRedirectUri
router.HandleFunc("/rs/clientRedirectUri/add", rh.AddRedirectURI).Methods("POST")
router.HandleFunc("/rs/clientRedirectUri/list/{clientId}", rh.GetRedirectURIList).Methods("GET")
router.HandleFunc("/rs/clientRedirectUri/delete/{id}", rh.DeleteRedirectURI).Methods("DELETE")
//clientRole
router.HandleFunc("/rs/clientRole/add", rh.AddRole).Methods("POST")
router.HandleFunc("/rs/clientRoleSuper/add", rh.AddRoleSuper).Methods("POST")
router.HandleFunc("/rs/clientRole/list/{clientId}", rh.GetRoleList).Methods("GET")
router.HandleFunc("/rs/clientRole/delete/{id}", rh.DeleteRole).Methods("DELETE")
//clientRoleUri
router.HandleFunc("/rs/clientRoleUri/add", rh.AddRoleURI).Methods("POST")
router.HandleFunc("/rs/clientRoleUri/list/{clientRoleId}", rh.GetRoleURIList).Methods("GET")
//---- added post delete for backwards compatibility
router.HandleFunc("/rs/clientRoleUri/delete", rh.DeleteRoleURI).Methods("POST")
router.HandleFunc("/rs/clientRoleUri/delete/{clientRoleId}/{clientAllowedUriId}", rh.DeleteRoleURI).Methods("DELETE")
//validate token
router.HandleFunc("/rs/token/validate", rh.ValidateAccessToken).Methods("POST")
router.HandleFunc("/rs/loglevel", rh.SetLogLevel).Methods("POST")
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/")))
fmt.Println("Starting server Oauth2 Server on " + port)
http.ListenAndServe(":"+port, router)
}
// [
// {
// "url":"/ulbora/rs/clientAllowedUri/add",
// "assets":[
// {
// "controlledAsset":"ulbora",
// "allowedRole":"superAdmin"
// }
// ]
// },
// {
// "url":"/ulbora/rs/clientAllowedUri/update",
// "assets":[
// {
// "controlledAsset":"ulbora",
// "allowedRole":"superAdmin"
// }
// ]
// },
// {
// "url":"/ulbora/rs/clientRole/add",
// "assets":[
// {
// "controlledAsset":"superAdmin",
// "allowedRole":"superAdmin"
// }
// ]
// }
// ]
// WwogICB7CiAgICAgICJ1cmwiOiIvdWxib3JhL3JzL2NsaWVudEFsbG93ZWRVcmkvYWRkIiwKICAgICAgImFzc2V0cyI6WwogICAgICAgICB7CiAgICAgICAgICAgICJjb250cm9sbGVkQXNzZXQiOiJ1bGJvcmEiLAogICAgICAgICAgICAiYWxsb3dlZFJvbGUiOiJzdXBlckFkbWluIgogICAgICAgICB9CiAgICAgIF0KICAgfSwKICAgewogICAgICAidXJsIjoiL3VsYm9yYS9ycy9jbGllbnRBbGxvd2VkVXJpL3VwZGF0ZSIsCiAgICAgICJhc3NldHMiOlsKICAgICAgICAgewogICAgICAgICAgICAiY29udHJvbGxlZEFzc2V0IjoidWxib3JhIiwKICAgICAgICAgICAgImFsbG93ZWRSb2xlIjoic3VwZXJBZG1pbiIKICAgICAgICAgfQogICAgICBdCiAgIH0sCiAgIHsKICAgICAgInVybCI6Ii91bGJvcmEvcnMvY2xpZW50Um9sZS9hZGQiLAogICAgICAiYXNzZXRzIjpbCiAgICAgICAgIHsKICAgICAgICAgICAgImNvbnRyb2xsZWRBc3NldCI6InN1cGVyQWRtaW4iLAogICAgICAgICAgICAiYWxsb3dlZFJvbGUiOiJzdXBlckFkbWluIgogICAgICAgICB9CiAgICAgIF0KICAgfQpd