diff --git a/server/constants/constants.go b/server/constants/constants.go index 73d6b42..125a2a9 100644 --- a/server/constants/constants.go +++ b/server/constants/constants.go @@ -13,7 +13,7 @@ var ( JWT_TYPE = "" JWT_SECRET = "" FRONTEND_URL = "" - SERVER_URL = "" + AUTHORIZER_DOMAIN = "" PORT = "8080" REDIS_URL = "" IS_PROD = false diff --git a/server/env.go b/server/env.go index 45825e0..1db7b84 100644 --- a/server/env.go +++ b/server/env.go @@ -47,7 +47,7 @@ func InitEnv() { constants.JWT_SECRET = os.Getenv("JWT_SECRET") constants.JWT_TYPE = os.Getenv("JWT_TYPE") constants.FRONTEND_URL = strings.TrimSuffix(os.Getenv("FRONTEND_URL"), "/") - constants.SERVER_URL = strings.TrimSuffix(os.Getenv("SERVER_URL"), "/") + constants.AUTHORIZER_DOMAIN = strings.TrimSuffix(os.Getenv("AUTHORIZER_DOMAIN"), "/") constants.PORT = os.Getenv("PORT") constants.REDIS_URL = os.Getenv("REDIS_URL") constants.COOKIE_NAME = os.Getenv("COOKIE_NAME") @@ -96,8 +96,8 @@ func InitEnv() { constants.COOKIE_NAME = "authorizer" } - if constants.SERVER_URL == "" { - constants.SERVER_URL = "http://localhost:8080" + if constants.AUTHORIZER_DOMAIN == "" { + constants.AUTHORIZER_DOMAIN = "http://localhost:8080" } if constants.DISABLE_BASIC_AUTHENTICATION == "" { diff --git a/server/handlers/oauthCallback.go b/server/handlers/oauthCallback.go index 38375a0..eba04bc 100644 --- a/server/handlers/oauthCallback.go +++ b/server/handlers/oauthCallback.go @@ -163,14 +163,17 @@ func processGithubUserInfo(state string, code string, c *gin.Context) error { return nil } -func OAuthCallbackHandler(provider enum.OAuthProvider) gin.HandlerFunc { +func OAuthCallbackHandler() gin.HandlerFunc { return func(c *gin.Context) { + provider := c.Param("oauth_provider") var err error - if provider == enum.GoogleProvider { + switch provider { + case enum.Google.String(): err = processGoogleUserInfo(c.Request.FormValue("state"), c.Request.FormValue("code"), c) - } - if provider == enum.GithubProvider { + case enum.Github.String(): err = processGithubUserInfo(c.Request.FormValue("state"), c.Request.FormValue("code"), c) + default: + err = fmt.Errorf(`invalid oauth provider`) } if err != nil { diff --git a/server/handlers/oauthLogin.go b/server/handlers/oauthLogin.go index 8b263ee..175ea33 100644 --- a/server/handlers/oauthLogin.go +++ b/server/handlers/oauthLogin.go @@ -10,20 +10,26 @@ import ( "github.com/google/uuid" ) -func OAuthLoginHandler(provider enum.OAuthProvider) gin.HandlerFunc { +func OAuthLoginHandler() gin.HandlerFunc { uuid := uuid.New() oauthStateString := uuid.String() return func(c *gin.Context) { - if provider == enum.GoogleProvider { + provider := c.Param("oauth_provider") + + switch provider { + case enum.Google.String(): session.SetToken(oauthStateString, enum.Google.String()) url := oauth.OAuthProvider.GoogleConfig.AuthCodeURL(oauthStateString) c.Redirect(http.StatusTemporaryRedirect, url) - } - if provider == enum.GithubProvider { + case enum.Github.String(): session.SetToken(oauthStateString, enum.Github.String()) url := oauth.OAuthProvider.GithubConfig.AuthCodeURL(oauthStateString) c.Redirect(http.StatusTemporaryRedirect, url) + default: + c.JSON(422, gin.H{ + "message": "Invalid oauth provider", + }) } } } diff --git a/server/main.go b/server/main.go index e304d72..87bb837 100644 --- a/server/main.go +++ b/server/main.go @@ -4,7 +4,6 @@ import ( "context" "github.com/authorizerdev/authorizer/server/db" - "github.com/authorizerdev/authorizer/server/enum" "github.com/authorizerdev/authorizer/server/handlers" "github.com/authorizerdev/authorizer/server/oauth" "github.com/authorizerdev/authorizer/server/session" @@ -40,6 +39,7 @@ func main() { InitEnv() db.InitDB() session.InitSession() + oauth.InitOAuth() r := gin.Default() r.Use(GinContextToContextMiddleware()) @@ -47,13 +47,7 @@ func main() { r.GET("/", handlers.PlaygroundHandler()) r.POST("/graphql", handlers.GraphqlHandler()) r.GET("/verify_email", handlers.VerifyEmailHandler()) - if oauth.OAuthProvider.GoogleConfig != nil { - r.GET("/login/google", handlers.OAuthLoginHandler(enum.GoogleProvider)) - r.GET("/callback/google", handlers.OAuthCallbackHandler(enum.GoogleProvider)) - } - if oauth.OAuthProvider.GithubConfig != nil { - r.GET("/login/github", handlers.OAuthLoginHandler(enum.GithubProvider)) - r.GET("/callback/github", handlers.OAuthCallbackHandler(enum.GithubProvider)) - } + r.GET("/login/:oauth_provider", handlers.OAuthLoginHandler()) + r.GET("/callback/:oauth_provider", handlers.OAuthCallbackHandler()) r.Run() } diff --git a/server/oauth/oauth.go b/server/oauth/oauth.go index 2d84e44..cc19800 100644 --- a/server/oauth/oauth.go +++ b/server/oauth/oauth.go @@ -1,6 +1,8 @@ package oauth import ( + "log" + "github.com/authorizerdev/authorizer/server/constants" "golang.org/x/oauth2" githubOAuth2 "golang.org/x/oauth2/github" @@ -15,21 +17,24 @@ type OAuthProviders struct { var OAuthProvider OAuthProviders -func init() { +func InitOAuth() { + log.Println("---> initializing auth") if constants.GOOGLE_CLIENT_ID != "" && constants.GOOGLE_CLIENT_SECRET != "" { + log.Println("---> initializing google auth") OAuthProvider.GoogleConfig = &oauth2.Config{ ClientID: constants.GOOGLE_CLIENT_ID, ClientSecret: constants.GOOGLE_CLIENT_SECRET, - RedirectURL: constants.SERVER_URL + "/callback/google", + RedirectURL: constants.AUTHORIZER_DOMAIN + "/callback/google", Endpoint: googleOAuth2.Endpoint, Scopes: []string{"https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile"}, } } if constants.GITHUB_CLIENT_ID != "" && constants.GITHUB_CLIENT_SECRET != "" { + log.Println("---> initializing github auth") OAuthProvider.GithubConfig = &oauth2.Config{ ClientID: constants.GITHUB_CLIENT_ID, ClientSecret: constants.GITHUB_CLIENT_SECRET, - RedirectURL: constants.SERVER_URL + "/callback/github", + RedirectURL: constants.AUTHORIZER_DOMAIN + "/callback/github", Endpoint: githubOAuth2.Endpoint, } } @@ -37,7 +42,7 @@ func init() { // OAuthProvider.FacebookConfig = &oauth2.Config{ // ClientID: constants.FACEBOOK_CLIENT_ID, // ClientSecret: constants.FACEBOOK_CLIENT_SECRET, - // RedirectURL: constants.SERVER_URL + "/callback/facebook/", + // RedirectURL: "/callback/facebook/", // Endpoint: facebookOAuth2.Endpoint, // } // } diff --git a/server/resolvers/token.go b/server/resolvers/token.go index ccf0e4e..9a4a910 100644 --- a/server/resolvers/token.go +++ b/server/resolvers/token.go @@ -3,7 +3,6 @@ package resolvers import ( "context" "fmt" - "log" "time" "github.com/authorizerdev/authorizer/server/db" @@ -14,8 +13,6 @@ import ( ) func Token(ctx context.Context) (*model.AuthResponse, error) { - metaInfo := utils.GetMetaInfo() - log.Println("=> meta", metaInfo) var res *model.AuthResponse gc, err := utils.GinContextFromContext(ctx) diff --git a/server/utils/cookie.go b/server/utils/cookie.go index 67e5084..5342323 100644 --- a/server/utils/cookie.go +++ b/server/utils/cookie.go @@ -1,9 +1,7 @@ package utils import ( - "log" "net/http" - "net/url" "github.com/authorizerdev/authorizer/server/constants" "github.com/gin-gonic/gin" @@ -13,12 +11,8 @@ func SetCookie(gc *gin.Context, token string) { secure := true httpOnly := true - u, err := url.Parse(constants.SERVER_URL) - if err != nil { - log.Println("error getting server host") - } gc.SetSameSite(http.SameSiteNoneMode) - gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", u.Hostname(), secure, httpOnly) + gc.SetCookie(constants.COOKIE_NAME, token, 3600, "/", gc.Request.Host, secure, httpOnly) } func GetCookie(gc *gin.Context) (string, error) { @@ -38,11 +32,7 @@ func DeleteCookie(gc *gin.Context) { secure = false } - u, err := url.Parse(constants.SERVER_URL) - if err != nil { - log.Println("error getting server host") - } gc.SetSameSite(http.SameSiteNoneMode) - gc.SetCookie(constants.COOKIE_NAME, "", -1, "/", u.Hostname(), secure, httpOnly) + gc.SetCookie(constants.COOKIE_NAME, "", -1, "/", gc.Request.Host, secure, httpOnly) } diff --git a/server/utils/email.go b/server/utils/email.go index 13aabde..eb12ac1 100644 --- a/server/utils/email.go +++ b/server/utils/email.go @@ -26,7 +26,7 @@ func SendVerificationMail(toEmail, token string) error { Click here to verify - `, constants.SERVER_URL+"/verify_email"+"?token="+token) + `, constants.AUTHORIZER_DOMAIN+"/verify_email"+"?token="+token) bodyMessage := sender.WriteHTMLEmail(Receiver, Subject, message) return sender.SendMail(Receiver, Subject, bodyMessage)