Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibbp committed Dec 12, 2024
1 parent 2223924 commit 3dd4c47
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
19 changes: 0 additions & 19 deletions internal/auth/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,6 @@ func setTokenCookie(c echo.Context, name string, token string, expiration time.T
c.SetCookie(cookie)
}

// checkAccessToken checks if the JWT access token is valid.
func checkAccessToken(accessToken string) (*Claims, error) {
// Parse the token.
token, err := jwt.ParseWithClaims(accessToken, &Claims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(GetJWTSecret()), nil
})
if err != nil {
return nil, err
}

// Validate the token and return the custom claims.
claims, ok := token.Claims.(*Claims)
if !ok || !token.Valid {
return nil, err
}

return claims, nil
}

// JWTErrorChecker will be executed when user try to access a protected path.
func JWTErrorChecker(err error, c echo.Context) error {
// Redirects to the signIn form.
Expand Down
26 changes: 19 additions & 7 deletions internal/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,25 @@ type OAuthResponse struct {
UserInfo UserInfo
}

func generateSecureRandomString() string {
func generateSecureRandomString() (string, error) {
b := make([]byte, 32)
rand.Read(b)
return base64.URLEncoding.EncodeToString(b)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(b), nil
}

func (s *Service) OAuthRedirect(c echo.Context) error {
// generate state and nonce
state := generateSecureRandomString()
nonce := generateSecureRandomString()
state, err := generateSecureRandomString()
if err != nil {
return err
}
nonce, err := generateSecureRandomString()
if err != nil {
return err
}

stateCookie := new(http.Cookie)
stateCookie.Name = "oidc_state"
Expand All @@ -82,7 +91,7 @@ func (s *Service) OAuthRedirect(c echo.Context) error {
oauth2.SetAuthURLParam("nonce", nonce),
oauth2.AccessTypeOffline,
)
err := c.Redirect(http.StatusTemporaryRedirect, authURL)
err = c.Redirect(http.StatusTemporaryRedirect, authURL)
if err != nil {
return err
}
Expand Down Expand Up @@ -135,7 +144,10 @@ func (s *Service) OAuthCallback(c echo.Context) (*ent.User, error) {

// Debug claims in dev
if s.EnvConfig.Development {
debugOidcClaims(idToken)
err := debugOidcClaims(idToken)
if err != nil {
return nil, err
}
}

// Verify nonce to prevent replay attack
Expand Down
5 changes: 4 additions & 1 deletion internal/transport/http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func userFromContext(c echo.Context) *ent.User {
userStr := c.Get("user")
user, ok := userStr.(*ent.User)
if !ok {
ErrorInvalidAccessTokenResponse(c)
err := ErrorInvalidAccessTokenResponse(c)
if err != nil {
return nil
}
}
return user
}

0 comments on commit 3dd4c47

Please sign in to comment.