Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rupinr committed Oct 28, 2024
1 parent 7f49223 commit 318d865
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"crypto/rsa"
"errors"
"fmt"
"net/http"
"news-master/app"
Expand Down Expand Up @@ -67,39 +68,48 @@ func loadPublicKey() (*rsa.PublicKey, error) {
return jwt.ParseRSAPublicKeyFromPEM(publicKeyData)
}

func (token *Token) validateAdminToken() *DecodedUser {
func (token *Token) validateAdminToken() (*DecodedUser, error) {
user := defaultDecodedUser()
if token.Value == app.Config.AdminToken {
user.Admin = true
user.Valid = true
return user, nil
} else {
return nil, errors.New("invalid admin token")
}
return user

}

func (token *Token) validateSubscriberToken() *DecodedUser {
user, _ := ValidateJWT(token.Value)
return user
func (token *Token) validateSubscriberToken() (*DecodedUser, error) {
user, error := ValidateJWT(token.Value)
return user, error

}

func ValidateAdminToken(token Token) *DecodedUser {
func ValidateAdminToken(token Token) (*DecodedUser, error) {
return token.validateAdminToken()
}

func ValidateSubscriberToken(token Token) *DecodedUser {
func ValidateSubscriberToken(token Token) (*DecodedUser, error) {
return token.validateSubscriberToken()
}

func AuthMiddleware(validateToken func(Token) *DecodedUser) gin.HandlerFunc {
func AuthMiddleware(validateToken func(Token) (*DecodedUser, error)) gin.HandlerFunc {
return func(c *gin.Context) {
token := Token{Value: c.Request.Header.Get("Authorization")}
user := validateToken(token)

if token.Value == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Missing token"})
c.Abort()
return
}
user, err := validateToken(token)

if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
c.Abort()
return
}

if !user.Valid {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
c.Abort()
Expand Down

0 comments on commit 318d865

Please sign in to comment.