2021-07-17 21:59:50 +05:30
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-08-04 15:55:13 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2021-07-23 21:57:44 +05:30
|
|
|
"github.com/authorizerdev/authorizer/server/enum"
|
|
|
|
"github.com/authorizerdev/authorizer/server/oauth"
|
|
|
|
"github.com/authorizerdev/authorizer/server/session"
|
2021-07-17 21:59:50 +05:30
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2021-08-04 12:18:57 +05:30
|
|
|
// set host in the oauth state that is useful for redirecting
|
2021-07-17 21:59:50 +05:30
|
|
|
|
2021-08-04 12:18:57 +05:30
|
|
|
func OAuthLoginHandler() gin.HandlerFunc {
|
2021-07-17 21:59:50 +05:30
|
|
|
return func(c *gin.Context) {
|
2021-08-04 12:18:57 +05:30
|
|
|
// TODO validate redirect URL
|
|
|
|
redirectURL := c.Query("redirectURL")
|
|
|
|
|
|
|
|
if redirectURL == "" {
|
|
|
|
c.JSON(400, gin.H{
|
|
|
|
"error": "invalid redirect url",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
uuid := uuid.New()
|
|
|
|
oauthStateString := uuid.String() + "___" + redirectURL
|
|
|
|
|
2021-07-28 16:38:55 +05:30
|
|
|
provider := c.Param("oauth_provider")
|
|
|
|
|
|
|
|
switch provider {
|
|
|
|
case enum.Google.String():
|
2021-07-17 21:59:50 +05:30
|
|
|
session.SetToken(oauthStateString, enum.Google.String())
|
2021-08-04 15:55:13 +05:30
|
|
|
// during the init of OAuthProvider authorizer url might be empty
|
|
|
|
oauth.OAuthProvider.GoogleConfig.RedirectURL = constants.AUTHORIZER_URL + "/oauth_callback/google"
|
2021-07-17 21:59:50 +05:30
|
|
|
url := oauth.OAuthProvider.GoogleConfig.AuthCodeURL(oauthStateString)
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, url)
|
2021-07-28 16:38:55 +05:30
|
|
|
case enum.Github.String():
|
2021-07-18 04:48:42 +05:30
|
|
|
session.SetToken(oauthStateString, enum.Github.String())
|
2021-08-04 15:55:13 +05:30
|
|
|
oauth.OAuthProvider.GithubConfig.RedirectURL = constants.AUTHORIZER_URL + "/oauth_callback/github"
|
2021-07-18 04:48:42 +05:30
|
|
|
url := oauth.OAuthProvider.GithubConfig.AuthCodeURL(oauthStateString)
|
|
|
|
c.Redirect(http.StatusTemporaryRedirect, url)
|
2021-07-28 16:38:55 +05:30
|
|
|
default:
|
|
|
|
c.JSON(422, gin.H{
|
|
|
|
"message": "Invalid oauth provider",
|
|
|
|
})
|
2021-07-18 04:48:42 +05:30
|
|
|
}
|
2021-07-17 21:59:50 +05:30
|
|
|
}
|
|
|
|
}
|