-
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.
Signed-off-by: PratikforCoding <kotalpratik@gmail.com>
- Loading branch information
1 parent
4d7656a
commit db3be15
Showing
10 changed files
with
210 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
var ErrorNoAuthHeaderIncluded = errors.New("not auth header included in request") | ||
|
||
func MakeAccessToken(id string, jwtSecret string, expiresIn time.Duration) (string, error) { | ||
signingKey := []byte(jwtSecret) | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.RegisteredClaims{ | ||
IssuedAt: jwt.NewNumericDate(time.Now().UTC()), | ||
ExpiresAt: jwt.NewNumericDate(time.Now().UTC().Add(expiresIn)), | ||
Subject: id, | ||
Issuer: "busofact-access", // Match the issuer here | ||
}) | ||
|
||
return token.SignedString(signingKey) | ||
} | ||
|
||
func MakeRefreshToken(id string, jwtSecret string, expiresIn time.Duration) (string, error) { | ||
signingKey := []byte(jwtSecret) | ||
|
||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.RegisteredClaims{ | ||
IssuedAt: jwt.NewNumericDate(time.Now().UTC()), | ||
ExpiresAt: jwt.NewNumericDate(time.Now().UTC().Add(expiresIn)), | ||
Subject: id, | ||
Issuer: "busofact-refresh", // Match the issuer here | ||
}) | ||
|
||
return token.SignedString(signingKey) | ||
} | ||
|
||
func GetTokenFromCookie(r *http.Request) (string, error) { | ||
// Get access_token from cookie | ||
cookie, err := r.Cookie("access_token") | ||
if err != nil { | ||
if err == http.ErrNoCookie { | ||
// If the cookie is not set, return an unauthorized status | ||
return "", errors.New("no cookie included in request") | ||
} | ||
|
||
return "", errors.New("couldn't get cookie from request") | ||
} | ||
|
||
return cookie.Value, nil | ||
} | ||
|
||
func ValidateJWT(tokenString, tokenSecret string) (string, error) { | ||
claimsStruct := jwt.RegisteredClaims{} | ||
token, err := jwt.ParseWithClaims( | ||
tokenString, | ||
&claimsStruct, | ||
func(token *jwt.Token) (interface{}, error) {return []byte(tokenSecret), nil}, | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
issuer, err := token.Claims.GetIssuer() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if issuer != "busofact-access" { | ||
return "", errors.New("token is refresh token") | ||
} | ||
|
||
userIDString, err := token.Claims.GetSubject() | ||
if err != nil { | ||
return "", err | ||
} | ||
return userIDString, 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
Oops, something went wrong.