-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
235 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"vibrain/internal/pkg/config" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
var signingMethod = jwt.SigningMethodHS256 | ||
|
||
type JwtUser struct { | ||
UserID string `json:"user_id"` | ||
OAuthProvider string `json:"oauth_provider,omitempty"` | ||
AccessToken string `json:"access_token"` | ||
TokenType string `json:"token_type,omitempty"` | ||
Expiry time.Time `json:"expiry,omitempty"` | ||
} | ||
|
||
func getJWTSecret() []byte { | ||
return []byte(config.Settings.JWTSecret) | ||
} | ||
|
||
func GenerateJWT(user JwtUser) (string, error) { | ||
token := jwt.NewWithClaims(signingMethod, jwt.MapClaims{ | ||
"exp": time.Now().Add(time.Hour * 24).Unix(), | ||
"user": user, | ||
}) | ||
return token.SignedString(getJWTSecret()) | ||
} | ||
|
||
func ValidateJWT(tokenString string) (*JwtUser, error) { | ||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { | ||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | ||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) | ||
} | ||
return getJWTSecret(), nil | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid jwt token: %w", err) | ||
} | ||
|
||
claim := token.Claims.(jwt.MapClaims) | ||
user, ok := claim["user"] | ||
if !ok { | ||
return nil, fmt.Errorf("user claim not found") | ||
} | ||
|
||
return user.(*JwtUser), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"vibrain/internal/pkg/config" | ||
|
||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/github" | ||
) | ||
|
||
func getOAuth2Config(provider string) (*oauth2.Config, error) { | ||
if strings.ToLower(provider) == "github" { | ||
return &oauth2.Config{ | ||
ClientID: config.Settings.OAuthGithubKey, | ||
ClientSecret: config.Settings.OAuthGithubSecret, | ||
Endpoint: github.Endpoint, | ||
RedirectURL: fmt.Sprintf("%s/oauth/github/callback", config.Settings.Fqdn), | ||
Scopes: []string{"user:email"}, | ||
}, nil | ||
} | ||
|
||
return nil, fmt.Errorf("oauth provider '%s' not found", provider) | ||
} | ||
|
||
func GetOAuth2RedirectURL(ctx context.Context, provider string) (string, error) { | ||
cfg, err := getOAuth2Config(provider) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get oauth config: %w", err) | ||
} | ||
authCodeUrl := cfg.AuthCodeURL("state:"+provider, oauth2.AccessTypeOnline) | ||
return authCodeUrl, nil | ||
} | ||
|
||
func GetOAuth2Token(ctx context.Context, provider, code string) (*oauth2.Token, error) { | ||
cfg, err := getOAuth2Config(provider) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get oauth config: %w", err) | ||
} | ||
token, err := cfg.Exchange(ctx, code) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to exchange oauth code: %w", err) | ||
} | ||
return token, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"vibrain/internal/pkg/auth" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func LoginHandler(c echo.Context) error { | ||
// return web/login.html page | ||
return c.File("web/login.html") | ||
} | ||
|
||
func OAuthLoginHandler(c echo.Context) error { | ||
provider := c.Param("provider") | ||
ctx := c.Request().Context() | ||
redirectUrl, err := auth.GetOAuth2RedirectURL(ctx, provider) | ||
if err != nil { | ||
return ErrorResponse(c, http.StatusInternalServerError, fmt.Errorf("failed to get oauth redirect url: %w", err)) | ||
} | ||
|
||
return c.Redirect(http.StatusTemporaryRedirect, redirectUrl) | ||
} | ||
|
||
func OAuthCallbackHandler(c echo.Context) error { | ||
provider := c.Param("provider") | ||
code := c.QueryParam("code") | ||
ctx := c.Request().Context() | ||
|
||
token, err := auth.GetOAuth2Token(ctx, provider, code) | ||
if err != nil { | ||
return ErrorResponse(c, http.StatusInternalServerError, fmt.Errorf("failed to get oauth token: %w", err)) | ||
} | ||
|
||
// TODO: get user info from database | ||
userId := "userId" | ||
|
||
jwtUser := auth.JwtUser{ | ||
UserID: userId, | ||
OAuthProvider: provider, | ||
AccessToken: token.AccessToken, | ||
TokenType: token.TokenType, | ||
Expiry: token.Expiry, | ||
} | ||
|
||
jwtToken, err := auth.GenerateJWT(jwtUser) | ||
if err != nil { | ||
return ErrorResponse(c, http.StatusInternalServerError, fmt.Errorf("failed to generate jwt token: %w", err)) | ||
} | ||
|
||
// write jwt token to cookie | ||
cookie := new(http.Cookie) | ||
cookie.Name = "token" | ||
cookie.Value = jwtToken | ||
cookie.Expires = token.Expiry | ||
c.SetCookie(cookie) | ||
return JsonResponse(c, http.StatusOK, map[string]interface{}{ | ||
"jwt_token": jwtToken, | ||
"jwt_user": jwtUser, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.