fix: rename server_url -> authorizer_domain

This commit is contained in:
Lakhan Samani 2021-07-28 16:38:55 +05:30
parent 030cf9eeee
commit 464f9105c4
9 changed files with 36 additions and 41 deletions

View File

@ -13,7 +13,7 @@ var (
JWT_TYPE = ""
JWT_SECRET = ""
FRONTEND_URL = ""
SERVER_URL = ""
AUTHORIZER_DOMAIN = ""
PORT = "8080"
REDIS_URL = ""
IS_PROD = false

View File

@ -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 == "" {

View File

@ -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 {

View File

@ -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",
})
}
}
}

View File

@ -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()
}

View File

@ -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,
// }
// }

View File

@ -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)

View File

@ -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)
}

View File

@ -26,7 +26,7 @@ func SendVerificationMail(toEmail, token string) error {
<a href="%s">Click here to verify</a>
</body>
</html>
`, 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)