|
|
|
@@ -64,22 +64,55 @@ func AuthorizeHandler() gin.HandlerFunc {
|
|
|
|
|
|
|
|
|
|
if err := validateAuthorizeRequest(responseType, responseMode, clientID, state, codeChallenge); err != nil {
|
|
|
|
|
log.Debug("invalid authorization request: ", err)
|
|
|
|
|
gc.JSON(http.StatusBadRequest, gin.H{"error": err})
|
|
|
|
|
gc.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log := log.WithFields(log.Fields{
|
|
|
|
|
"response_mode": responseMode,
|
|
|
|
|
"response_type": responseType,
|
|
|
|
|
"state": state,
|
|
|
|
|
"code_challenge": codeChallenge,
|
|
|
|
|
"scope": scope,
|
|
|
|
|
"redirect_uri": redirectURI,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// used for response mode query or fragment
|
|
|
|
|
loginState := "state=" + state + "&scope=" + strings.Join(scope, " ") + "&redirect_uri=" + redirectURI
|
|
|
|
|
loginURL := "/app?" + loginState
|
|
|
|
|
|
|
|
|
|
if responseMode == constants.ResponseModeFragment {
|
|
|
|
|
loginURL = "/app#" + loginState
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loginError := map[string]interface{}{
|
|
|
|
|
"error": "login_required",
|
|
|
|
|
"error_description": "Login is required",
|
|
|
|
|
if state == "" {
|
|
|
|
|
handleResponse(gc, responseMode, loginURL, redirectURI, map[string]interface{}{
|
|
|
|
|
"type": "authorization_response",
|
|
|
|
|
"response": map[string]interface{}{
|
|
|
|
|
"error": "state_required",
|
|
|
|
|
"error_description": "state is required",
|
|
|
|
|
},
|
|
|
|
|
}, http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if responseType == constants.ResponseTypeCode && codeChallenge == "" {
|
|
|
|
|
handleResponse(gc, responseMode, loginURL, redirectURI, map[string]interface{}{
|
|
|
|
|
"type": "authorization_response",
|
|
|
|
|
"response": map[string]interface{}{
|
|
|
|
|
"error": "code_challenge_required",
|
|
|
|
|
"error_description": "code challenge is required",
|
|
|
|
|
},
|
|
|
|
|
}, http.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loginError := map[string]interface{}{
|
|
|
|
|
"type": "authorization_response",
|
|
|
|
|
"response": map[string]interface{}{
|
|
|
|
|
"error": "login_required",
|
|
|
|
|
"error_description": "Login is required",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
sessionToken, err := cookie.GetSession(gc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Debug("GetSession failed: ", err)
|
|
|
|
@@ -94,13 +127,17 @@ func AuthorizeHandler() gin.HandlerFunc {
|
|
|
|
|
handleResponse(gc, responseMode, loginURL, redirectURI, loginError, http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID := claims.Subject
|
|
|
|
|
user, err := db.Provider.GetUserByID(gc, userID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Debug("GetUserByID failed: ", err)
|
|
|
|
|
handleResponse(gc, responseMode, loginURL, redirectURI, map[string]interface{}{
|
|
|
|
|
"error": "signup_required",
|
|
|
|
|
"error_description": "Sign up required",
|
|
|
|
|
"type": "authorization_response",
|
|
|
|
|
"response": map[string]interface{}{
|
|
|
|
|
"error": "signup_required",
|
|
|
|
|
"error_description": "Sign up required",
|
|
|
|
|
},
|
|
|
|
|
}, http.StatusOK)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
@@ -137,30 +174,40 @@ func AuthorizeHandler() gin.HandlerFunc {
|
|
|
|
|
|
|
|
|
|
// in case, response type is code and user is already logged in send the code and state
|
|
|
|
|
// and cookie session will already be rolled over and set
|
|
|
|
|
if responseMode == constants.ResponseModeFormPost {
|
|
|
|
|
gc.HTML(http.StatusOK, authorizeFormPostTemplate, gin.H{
|
|
|
|
|
"target_origin": redirectURI,
|
|
|
|
|
"authorization_response": map[string]interface{}{
|
|
|
|
|
"type": "authorization_response",
|
|
|
|
|
"response": map[string]string{
|
|
|
|
|
"code": code,
|
|
|
|
|
"state": state,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
gc.HTML(http.StatusOK, authorizeWebMessageTemplate, gin.H{
|
|
|
|
|
"target_origin": redirectURI,
|
|
|
|
|
"authorization_response": map[string]interface{}{
|
|
|
|
|
"type": "authorization_response",
|
|
|
|
|
"response": map[string]string{
|
|
|
|
|
"code": code,
|
|
|
|
|
"state": state,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
// gc.HTML(http.StatusOK, authorizeWebMessageTemplate, gin.H{
|
|
|
|
|
// "target_origin": redirectURI,
|
|
|
|
|
// "authorization_response": map[string]interface{}{
|
|
|
|
|
// "type": "authorization_response",
|
|
|
|
|
// "response": map[string]string{
|
|
|
|
|
// "code": code,
|
|
|
|
|
// "state": state,
|
|
|
|
|
// },
|
|
|
|
|
// },
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
params := "code=" + code + "&state=" + state
|
|
|
|
|
if responseMode == constants.ResponseModeQuery {
|
|
|
|
|
if strings.Contains(redirectURI, "?") {
|
|
|
|
|
redirectURI = redirectURI + "&" + params
|
|
|
|
|
} else {
|
|
|
|
|
redirectURI = redirectURI + "?" + params
|
|
|
|
|
}
|
|
|
|
|
} else if responseMode == constants.ResponseModeFragment {
|
|
|
|
|
if strings.Contains(redirectURI, "#") {
|
|
|
|
|
redirectURI = redirectURI + "&" + params
|
|
|
|
|
} else {
|
|
|
|
|
redirectURI = redirectURI + "#" + params
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleResponse(gc, responseMode, loginURL, redirectURI, map[string]interface{}{
|
|
|
|
|
"type": "authorization_response",
|
|
|
|
|
"response": map[string]interface{}{
|
|
|
|
|
"code": code,
|
|
|
|
|
"state": state,
|
|
|
|
|
},
|
|
|
|
|
}, http.StatusOK)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -244,27 +291,17 @@ func validateAuthorizeRequest(responseType, responseMode, clientID, state, codeC
|
|
|
|
|
return fmt.Errorf("invalid response mode %s. 'query', 'fragment', 'form_post' and 'web_message' are valid response_mode", responseMode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if responseType == constants.ResponseTypeCode && strings.TrimSpace(codeChallenge) == "" {
|
|
|
|
|
return fmt.Errorf("code_challenge is required for %s '%s'", responseType, constants.ResponseTypeCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if client, err := memorystore.Provider.GetStringStoreEnvVariable(constants.EnvKeyClientID); client != clientID || err != nil {
|
|
|
|
|
return fmt.Errorf("invalid client_id %s", clientID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.TrimSpace(state) == "" {
|
|
|
|
|
return fmt.Errorf("state is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleResponse(gc *gin.Context, responseMode, loginURI, redirectURI string, data map[string]interface{}, httpStatusCode int) {
|
|
|
|
|
isAuthenticationRequired := false
|
|
|
|
|
if val, ok := data["error"]; ok {
|
|
|
|
|
if val == "login_required" || val == "signup_required" {
|
|
|
|
|
isAuthenticationRequired = true
|
|
|
|
|
}
|
|
|
|
|
if _, ok := data["response"].(map[string]interface{})["error"]; ok {
|
|
|
|
|
isAuthenticationRequired = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch responseMode {
|
|
|
|
@@ -284,7 +321,7 @@ func handleResponse(gc *gin.Context, responseMode, loginURI, redirectURI string,
|
|
|
|
|
case constants.ResponseModeFormPost:
|
|
|
|
|
gc.HTML(httpStatusCode, authorizeFormPostTemplate, gin.H{
|
|
|
|
|
"target_origin": redirectURI,
|
|
|
|
|
"authorization_response": data,
|
|
|
|
|
"authorization_response": data["response"],
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|