|
@@ -1,6 +1,7 @@
|
|
|
package oauth2
|
|
|
|
|
|
import (
|
|
|
+ "context"
|
|
|
"encoding/json"
|
|
|
"log"
|
|
|
"net/http"
|
|
@@ -17,21 +18,19 @@ import (
|
|
|
type OauthHandler struct {
|
|
|
googleOauthConfig *oauth2.Config
|
|
|
syncDb *syncdb.SyncDB
|
|
|
- oauthStateString string
|
|
|
- DefaultUserGroup string
|
|
|
ag *auth.AuthAgent
|
|
|
reg *reg.RegisterHandler
|
|
|
coredb *db.Database
|
|
|
- config *Config
|
|
|
}
|
|
|
|
|
|
type Config struct {
|
|
|
- Enabled bool `json:"enabled"`
|
|
|
- IDP string `json:"idp"`
|
|
|
- RedirectURL string `json:"redirect_url"`
|
|
|
- ClientID string `json:"client_id"`
|
|
|
- ClientSecret string `json:"client_secret"`
|
|
|
- DefaultUserGroup string `json:"default_user_group"`
|
|
|
+ Enabled bool `json:"enabled"`
|
|
|
+ AutoRedirect bool `json:"auto_redirect"`
|
|
|
+ IDP string `json:"idp"`
|
|
|
+ RedirectURL string `json:"redirect_url"`
|
|
|
+ ServerURL string `json:"server_url"`
|
|
|
+ ClientID string `json:"client_id"`
|
|
|
+ ClientSecret string `json:"client_secret"`
|
|
|
}
|
|
|
|
|
|
//NewOauthHandler xxx
|
|
@@ -50,11 +49,10 @@ func NewOauthHandler(authAgent *auth.AuthAgent, register *reg.RegisterHandler, c
|
|
|
Scopes: getScope(coreDb),
|
|
|
Endpoint: getEndpoint(coreDb),
|
|
|
},
|
|
|
- DefaultUserGroup: readSingleConfig("defaultusergroup", coreDb),
|
|
|
- ag: authAgent,
|
|
|
- syncDb: syncdb.NewSyncDB(),
|
|
|
- reg: register,
|
|
|
- coredb: coreDb,
|
|
|
+ ag: authAgent,
|
|
|
+ syncDb: syncdb.NewSyncDB(),
|
|
|
+ reg: register,
|
|
|
+ coredb: coreDb,
|
|
|
}
|
|
|
|
|
|
return &NewlyCreatedOauthHandler
|
|
@@ -62,14 +60,21 @@ func NewOauthHandler(authAgent *auth.AuthAgent, register *reg.RegisterHandler, c
|
|
|
|
|
|
//HandleOauthLogin xxx
|
|
|
func (oh *OauthHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
+ enabled := oh.readSingleConfig("enabled")
|
|
|
+ if enabled == "" || enabled == "false" {
|
|
|
+ sendTextResponse(w, "OAuth disabled")
|
|
|
+ return
|
|
|
+ }
|
|
|
//add cookies
|
|
|
- redirect, e := r.URL.Query()["redirect"]
|
|
|
+ redirect, err := mv(r, "redirect", false)
|
|
|
+ //store the redirect url to the sync map
|
|
|
uuid := ""
|
|
|
- if !e || len(redirect[0]) < 1 {
|
|
|
+ if err != nil {
|
|
|
uuid = oh.syncDb.Store("/")
|
|
|
} else {
|
|
|
- uuid = oh.syncDb.Store(redirect[0])
|
|
|
+ uuid = oh.syncDb.Store(redirect)
|
|
|
}
|
|
|
+ //store the key to client
|
|
|
oh.addCookie(w, "uuid_login", uuid, 30*time.Minute)
|
|
|
//handle redirect
|
|
|
url := oh.googleOauthConfig.AuthCodeURL(uuid)
|
|
@@ -78,6 +83,11 @@ func (oh *OauthHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
//OauthAuthorize xxx
|
|
|
func (oh *OauthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request) {
|
|
|
+ enabled := oh.readSingleConfig("enabled")
|
|
|
+ if enabled == "" || enabled == "false" {
|
|
|
+ sendTextResponse(w, "OAuth disabled")
|
|
|
+ return
|
|
|
+ }
|
|
|
//read the uuid(aka the state parameter)
|
|
|
uuid, err := r.Cookie("uuid_login")
|
|
|
if err != nil {
|
|
@@ -85,52 +95,85 @@ func (oh *OauthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- state := r.FormValue("state")
|
|
|
+ state, err := mv(r, "state", true)
|
|
|
if state != uuid.Value {
|
|
|
sendTextResponse(w, "Invalid oauth state.")
|
|
|
return
|
|
|
}
|
|
|
+ if err != nil {
|
|
|
+ sendTextResponse(w, "Invalid state parameter.")
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- code := r.FormValue("code")
|
|
|
- token, err := oh.googleOauthConfig.Exchange(oauth2.NoContext, code)
|
|
|
+ code, err := mv(r, "code", true)
|
|
|
+ if err != nil {
|
|
|
+ sendTextResponse(w, "Invalid state parameter.")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ //exchange the infromation to get code
|
|
|
+ token, err := oh.googleOauthConfig.Exchange(context.Background(), code)
|
|
|
if err != nil {
|
|
|
sendTextResponse(w, "Code exchange failed.")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ //get user info
|
|
|
username, err := getUserInfo(token.AccessToken, oh.coredb)
|
|
|
if err != nil {
|
|
|
+ oh.ag.Logger.LogAuthByRequestInfo(username, r.RemoteAddr, time.Now().Unix(), false, "web")
|
|
|
sendTextResponse(w, "Failed to obtain user info.")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if !oh.ag.UserExists(username) {
|
|
|
//register user if not already exists
|
|
|
- //random pwd to prevent ppl bypassing the OAuth handler
|
|
|
+ //if registration is closed, return error message.
|
|
|
+ //also makr the login as fail.
|
|
|
if oh.reg.AllowRegistry {
|
|
|
- http.Redirect(w, r, "/public/register/register.system?user="+username, 302)
|
|
|
+ oh.ag.Logger.LogAuthByRequestInfo(username, r.RemoteAddr, time.Now().Unix(), false, "web")
|
|
|
+ http.Redirect(w, r, "/public/register/register.system?user="+username, http.StatusFound)
|
|
|
} else {
|
|
|
+ oh.ag.Logger.LogAuthByRequestInfo(username, r.RemoteAddr, time.Now().Unix(), false, "web")
|
|
|
sendHTMLResponse(w, "You are not allowed to register in this system. <a href=\"/\">Back</a>")
|
|
|
}
|
|
|
} else {
|
|
|
log.Println(username + " logged in via OAuth.")
|
|
|
oh.ag.LoginUserByRequest(w, r, username, true)
|
|
|
+ oh.ag.Logger.LogAuthByRequestInfo(username, r.RemoteAddr, time.Now().Unix(), true, "web")
|
|
|
//clear the cooke
|
|
|
oh.addCookie(w, "uuid_login", "-invaild-", -1)
|
|
|
//read the value from db and delete it from db
|
|
|
url := oh.syncDb.Read(uuid.Value)
|
|
|
oh.syncDb.Delete(uuid.Value)
|
|
|
//redirect to the desired page
|
|
|
- http.Redirect(w, r, url, 302)
|
|
|
+ http.Redirect(w, r, url, http.StatusFound)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//CheckOAuth check if oauth is enabled
|
|
|
func (oh *OauthHandler) CheckOAuth(w http.ResponseWriter, r *http.Request) {
|
|
|
+ enabledB := false
|
|
|
enabled := oh.readSingleConfig("enabled")
|
|
|
- if enabled == "" {
|
|
|
- enabled = "false"
|
|
|
+ if enabled == "true" {
|
|
|
+ enabledB = true
|
|
|
+ }
|
|
|
+
|
|
|
+ autoredirectB := false
|
|
|
+ autoredirect := oh.readSingleConfig("autoredirect")
|
|
|
+ if autoredirect == "true" {
|
|
|
+ autoredirectB = true
|
|
|
+ }
|
|
|
+
|
|
|
+ type returnFormat struct {
|
|
|
+ Enabled bool `json:"enabled"`
|
|
|
+ AutoRedirect bool `json:"auto_redirect"`
|
|
|
+ }
|
|
|
+ json, err := json.Marshal(returnFormat{Enabled: enabledB, AutoRedirect: autoredirectB})
|
|
|
+ if err != nil {
|
|
|
+ sendErrorResponse(w, "Error occurred while marshalling JSON response")
|
|
|
}
|
|
|
- sendJSONResponse(w, enabled)
|
|
|
+ sendJSONResponse(w, string(json))
|
|
|
}
|
|
|
|
|
|
//https://golangcode.com/add-a-http-cookie/
|
|
@@ -145,23 +188,36 @@ func (oh *OauthHandler) addCookie(w http.ResponseWriter, name, value string, ttl
|
|
|
}
|
|
|
|
|
|
func (oh *OauthHandler) ReadConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
- enabled, _ := strconv.ParseBool(oh.readSingleConfig("enabled"))
|
|
|
+ enabled, err := strconv.ParseBool(oh.readSingleConfig("enabled"))
|
|
|
+ if err != nil {
|
|
|
+ sendTextResponse(w, "Invalid config value [key=enabled].")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ autoredirect, err := strconv.ParseBool(oh.readSingleConfig("autoredirect"))
|
|
|
+ if err != nil {
|
|
|
+ sendTextResponse(w, "Invalid config value [key=autoredirect].")
|
|
|
+ return
|
|
|
+ }
|
|
|
idp := oh.readSingleConfig("idp")
|
|
|
redirecturl := oh.readSingleConfig("redirecturl")
|
|
|
+ serverurl := oh.readSingleConfig("serverurl")
|
|
|
clientid := oh.readSingleConfig("clientid")
|
|
|
clientsecret := oh.readSingleConfig("clientsecret")
|
|
|
- defaultusergroup := oh.readSingleConfig("defaultusergroup")
|
|
|
|
|
|
config, err := json.Marshal(Config{
|
|
|
- Enabled: enabled,
|
|
|
- IDP: idp,
|
|
|
- RedirectURL: redirecturl,
|
|
|
- ClientID: clientid,
|
|
|
- ClientSecret: clientsecret,
|
|
|
- DefaultUserGroup: defaultusergroup,
|
|
|
+ Enabled: enabled,
|
|
|
+ AutoRedirect: autoredirect,
|
|
|
+ IDP: idp,
|
|
|
+ ServerURL: serverurl,
|
|
|
+ RedirectURL: redirecturl,
|
|
|
+ ClientID: clientid,
|
|
|
+ ClientSecret: clientsecret,
|
|
|
})
|
|
|
if err != nil {
|
|
|
- empty, _ := json.Marshal(Config{})
|
|
|
+ empty, err := json.Marshal(Config{})
|
|
|
+ if err != nil {
|
|
|
+ sendErrorResponse(w, "Error while marshalling config")
|
|
|
+ }
|
|
|
sendJSONResponse(w, string(empty))
|
|
|
}
|
|
|
sendJSONResponse(w, string(config))
|
|
@@ -170,11 +226,14 @@ func (oh *OauthHandler) ReadConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
func (oh *OauthHandler) WriteConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
enabled, err := mv(r, "enabled", true)
|
|
|
if err != nil {
|
|
|
- sendErrorResponse(w, "enabled field can't be empty'")
|
|
|
+ sendErrorResponse(w, "enabled field can't be empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ autoredirect, err := mv(r, "autoredirect", true)
|
|
|
+ if err != nil {
|
|
|
+ sendErrorResponse(w, "enabled field can't be empty")
|
|
|
return
|
|
|
}
|
|
|
-
|
|
|
- oh.coredb.Write("oauth", "enabled", enabled)
|
|
|
|
|
|
showError := true
|
|
|
if enabled != "true" {
|
|
@@ -184,44 +243,54 @@ func (oh *OauthHandler) WriteConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
idp, err := mv(r, "idp", true)
|
|
|
if err != nil {
|
|
|
if showError {
|
|
|
- sendErrorResponse(w, "idp field can't be empty'")
|
|
|
+ sendErrorResponse(w, "idp field can't be empty")
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
redirecturl, err := mv(r, "redirecturl", true)
|
|
|
if err != nil {
|
|
|
if showError {
|
|
|
- sendErrorResponse(w, "redirecturl field can't be empty'")
|
|
|
+ sendErrorResponse(w, "redirecturl field can't be empty")
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
- clientid, err := mv(r, "clientid", true)
|
|
|
+ serverurl, err := mv(r, "serverurl", true)
|
|
|
if err != nil {
|
|
|
if showError {
|
|
|
- sendErrorResponse(w, "clientid field can't be empty'")
|
|
|
- return
|
|
|
+ if idp != "Gitlab" {
|
|
|
+ serverurl = ""
|
|
|
+ } else {
|
|
|
+ sendErrorResponse(w, "serverurl field can't be empty")
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- clientsecret, err := mv(r, "clientsecret", true)
|
|
|
+ if idp != "Gitlab" {
|
|
|
+ serverurl = ""
|
|
|
+ }
|
|
|
+
|
|
|
+ clientid, err := mv(r, "clientid", true)
|
|
|
if err != nil {
|
|
|
if showError {
|
|
|
- sendErrorResponse(w, "clientsecret field can't be empty'")
|
|
|
+ sendErrorResponse(w, "clientid field can't be empty")
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
- defaultusergroup, err := mv(r, "defaultusergroup", true)
|
|
|
+ clientsecret, err := mv(r, "clientsecret", true)
|
|
|
if err != nil {
|
|
|
if showError {
|
|
|
- sendErrorResponse(w, "defaultusergroup field can't be empty'")
|
|
|
+ sendErrorResponse(w, "clientsecret field can't be empty")
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ oh.coredb.Write("oauth", "enabled", enabled)
|
|
|
+ oh.coredb.Write("oauth", "autoredirect", autoredirect)
|
|
|
oh.coredb.Write("oauth", "idp", idp)
|
|
|
oh.coredb.Write("oauth", "redirecturl", redirecturl)
|
|
|
+ oh.coredb.Write("oauth", "serverurl", serverurl)
|
|
|
oh.coredb.Write("oauth", "clientid", clientid)
|
|
|
oh.coredb.Write("oauth", "clientsecret", clientsecret)
|
|
|
- oh.coredb.Write("oauth", "defaultusergroup", defaultusergroup)
|
|
|
|
|
|
//update the information inside the oauth class
|
|
|
oh.googleOauthConfig = &oauth2.Config{
|