authorizer/server/handlers/oauth_login.go

93 lines
3.0 KiB
Go
Raw Normal View History

package handlers
import (
"net/http"
"strings"
"github.com/authorizerdev/authorizer/server/constants"
2022-01-17 06:02:13 +00:00
"github.com/authorizerdev/authorizer/server/envstore"
2021-07-23 16:27:44 +00:00
"github.com/authorizerdev/authorizer/server/oauth"
"github.com/authorizerdev/authorizer/server/sessionstore"
"github.com/authorizerdev/authorizer/server/utils"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
2022-01-17 06:02:13 +00:00
// OAuthLoginHandler set host in the oauth state that is useful for redirecting to oauth_callback
func OAuthLoginHandler() gin.HandlerFunc {
return func(c *gin.Context) {
2022-01-31 06:05:24 +00:00
hostname := utils.GetHost(c)
redirectURL := c.Query("redirectURL")
roles := c.Query("roles")
if redirectURL == "" {
c.JSON(400, gin.H{
"error": "invalid redirect url",
})
return
}
if roles != "" {
// validate role
rolesSplit := strings.Split(roles, ",")
// use protected roles verification for admin login only.
// though if not associated with user, it will be rejected from oauth_callback
2022-02-28 02:25:01 +00:00
if !utils.IsValidRoles(append([]string{}, append(envstore.EnvStoreObj.GetSliceStoreEnvVariable(constants.EnvKeyRoles), envstore.EnvStoreObj.GetSliceStoreEnvVariable(constants.EnvKeyProtectedRoles)...)...), rolesSplit) {
c.JSON(400, gin.H{
"error": "invalid role",
})
return
}
} else {
2022-02-28 02:25:01 +00:00
roles = strings.Join(envstore.EnvStoreObj.GetSliceStoreEnvVariable(constants.EnvKeyDefaultRoles), ",")
}
uuid := uuid.New()
oauthStateString := uuid.String() + "___" + redirectURL + "___" + roles
provider := c.Param("oauth_provider")
2021-12-03 17:25:27 +00:00
isProviderConfigured := true
switch provider {
2022-01-17 06:02:13 +00:00
case constants.SignupMethodGoogle:
2021-12-03 17:25:27 +00:00
if oauth.OAuthProviders.GoogleConfig == nil {
isProviderConfigured = false
break
}
2022-02-28 15:56:49 +00:00
sessionstore.SetState(oauthStateString, constants.SignupMethodGoogle)
// during the init of OAuthProvider authorizer url might be empty
2022-01-31 06:05:24 +00:00
oauth.OAuthProviders.GoogleConfig.RedirectURL = hostname + "/oauth_callback/google"
2021-12-03 17:25:27 +00:00
url := oauth.OAuthProviders.GoogleConfig.AuthCodeURL(oauthStateString)
c.Redirect(http.StatusTemporaryRedirect, url)
2022-01-17 06:02:13 +00:00
case constants.SignupMethodGithub:
2021-12-03 17:25:27 +00:00
if oauth.OAuthProviders.GithubConfig == nil {
isProviderConfigured = false
break
}
2022-02-28 15:56:49 +00:00
sessionstore.SetState(oauthStateString, constants.SignupMethodGithub)
2022-01-31 06:05:24 +00:00
oauth.OAuthProviders.GithubConfig.RedirectURL = hostname + "/oauth_callback/github"
2021-12-03 17:25:27 +00:00
url := oauth.OAuthProviders.GithubConfig.AuthCodeURL(oauthStateString)
2021-07-17 23:18:42 +00:00
c.Redirect(http.StatusTemporaryRedirect, url)
2022-01-17 06:02:13 +00:00
case constants.SignupMethodFacebook:
2021-12-03 17:25:27 +00:00
if oauth.OAuthProviders.FacebookConfig == nil {
isProviderConfigured = false
break
}
2022-02-28 15:56:49 +00:00
sessionstore.SetState(oauthStateString, constants.SignupMethodFacebook)
2022-01-31 06:05:24 +00:00
oauth.OAuthProviders.FacebookConfig.RedirectURL = hostname + "/oauth_callback/facebook"
2021-12-03 17:25:27 +00:00
url := oauth.OAuthProviders.FacebookConfig.AuthCodeURL(oauthStateString)
2021-09-04 22:27:29 +00:00
c.Redirect(http.StatusTemporaryRedirect, url)
default:
c.JSON(422, gin.H{
"message": "Invalid oauth provider",
})
2021-07-17 23:18:42 +00:00
}
2021-12-03 17:25:27 +00:00
if !isProviderConfigured {
c.JSON(422, gin.H{
"message": provider + " not configured",
})
}
}
}