2021-07-17 16:29:50 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-10-13 16:41:41 +00:00
|
|
|
"strings"
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2021-08-04 10:25:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/enum"
|
|
|
|
"github.com/authorizerdev/authorizer/server/oauth"
|
|
|
|
"github.com/authorizerdev/authorizer/server/session"
|
2021-09-20 05:06:26 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/utils"
|
2021-07-17 16:29:50 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
// set host in the oauth state that is useful for redirecting
|
2021-07-17 16:29:50 +00:00
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
func OAuthLoginHandler() gin.HandlerFunc {
|
2021-07-17 16:29:50 +00:00
|
|
|
return func(c *gin.Context) {
|
2021-08-04 06:48:57 +00:00
|
|
|
// TODO validate redirect URL
|
|
|
|
redirectURL := c.Query("redirectURL")
|
2021-10-13 16:41:41 +00:00
|
|
|
roles := c.Query("roles")
|
2021-08-04 06:48:57 +00:00
|
|
|
|
|
|
|
if redirectURL == "" {
|
|
|
|
c.JSON(400, gin.H{
|
|
|
|
"error": "invalid redirect url",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2021-09-20 05:06:26 +00:00
|
|
|
|
2021-10-13 16:41:41 +00:00
|
|
|
if roles != "" {
|
2021-09-20 05:06:26 +00:00
|
|
|
// validate role
|
2021-10-13 16:41:41 +00:00
|
|
|
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
|
2021-12-31 08:22:10 +00:00
|
|
|
if !utils.IsValidRoles(append([]string{}, append(constants.EnvData.ROLES, constants.EnvData.PROTECTED_ROLES...)...), rolesSplit) {
|
2021-09-20 05:06:26 +00:00
|
|
|
c.JSON(400, gin.H{
|
|
|
|
"error": "invalid role",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-31 08:22:10 +00:00
|
|
|
roles = strings.Join(constants.EnvData.DEFAULT_ROLES, ",")
|
2021-09-20 05:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
uuid := uuid.New()
|
2021-10-13 16:41:41 +00:00
|
|
|
oauthStateString := uuid.String() + "___" + redirectURL + "___" + roles
|
2021-08-04 06:48:57 +00:00
|
|
|
|
2021-07-28 11:08:55 +00:00
|
|
|
provider := c.Param("oauth_provider")
|
2021-12-03 17:25:27 +00:00
|
|
|
isProviderConfigured := true
|
2021-07-28 11:08:55 +00:00
|
|
|
switch provider {
|
|
|
|
case enum.Google.String():
|
2021-12-03 17:25:27 +00:00
|
|
|
if oauth.OAuthProviders.GoogleConfig == nil {
|
|
|
|
isProviderConfigured = false
|
|
|
|
break
|
|
|
|
}
|
2021-10-27 17:45:38 +00:00
|
|
|
session.SetSocailLoginState(oauthStateString, enum.Google.String())
|
2021-08-04 10:25:13 +00:00
|
|
|
// during the init of OAuthProvider authorizer url might be empty
|
2021-12-31 08:22:10 +00:00
|
|
|
oauth.OAuthProviders.GoogleConfig.RedirectURL = constants.EnvData.AUTHORIZER_URL + "/oauth_callback/google"
|
2021-12-03 17:25:27 +00:00
|
|
|
url := oauth.OAuthProviders.GoogleConfig.AuthCodeURL(oauthStateString)
|
2021-07-17 16:29:50 +00:00
|
|
|
c.Redirect(http.StatusTemporaryRedirect, url)
|
2021-07-28 11:08:55 +00:00
|
|
|
case enum.Github.String():
|
2021-12-03 17:25:27 +00:00
|
|
|
if oauth.OAuthProviders.GithubConfig == nil {
|
|
|
|
isProviderConfigured = false
|
|
|
|
break
|
|
|
|
}
|
2021-10-27 17:45:38 +00:00
|
|
|
session.SetSocailLoginState(oauthStateString, enum.Github.String())
|
2021-12-31 08:22:10 +00:00
|
|
|
oauth.OAuthProviders.GithubConfig.RedirectURL = constants.EnvData.AUTHORIZER_URL + "/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)
|
2021-09-04 22:27:29 +00:00
|
|
|
case enum.Facebook.String():
|
2021-12-03 17:25:27 +00:00
|
|
|
if oauth.OAuthProviders.FacebookConfig == nil {
|
|
|
|
isProviderConfigured = false
|
|
|
|
break
|
|
|
|
}
|
2021-10-27 17:45:38 +00:00
|
|
|
session.SetSocailLoginState(oauthStateString, enum.Facebook.String())
|
2021-12-31 08:22:10 +00:00
|
|
|
oauth.OAuthProviders.FacebookConfig.RedirectURL = constants.EnvData.AUTHORIZER_URL + "/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)
|
2021-07-28 11:08:55 +00:00
|
|
|
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",
|
|
|
|
})
|
|
|
|
}
|
2021-07-17 16:29:50 +00:00
|
|
|
}
|
|
|
|
}
|