-
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.
feat: add migrations and godoc comments
- Loading branch information
1 parent
4eafdce
commit a1e93b9
Showing
12 changed files
with
111 additions
and
117 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 |
---|---|---|
@@ -1,13 +1,9 @@ | ||
package models | ||
|
||
import "github.com/satori/uuid" | ||
|
||
type JwtPayload struct { | ||
ID uuid.UUID | ||
Login string | ||
} | ||
|
||
// UserLoginData represents user information for login and signup | ||
type UserLoginData struct { | ||
Login string `json:"login"` | ||
// Login stands for users nickname | ||
Login string `json:"login"` | ||
// Password stands for users password | ||
Password string `json:"password"` | ||
} |
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
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,54 @@ | ||
package jwt | ||
|
||
import ( | ||
"2024_1_TeaStealers/internal/models" | ||
"errors" | ||
"fmt" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/satori/uuid" | ||
"os" | ||
"time" | ||
) | ||
|
||
// GenerateToken returns a new JWT token for the given user. | ||
func GenerateToken(user *models.User) (string, time.Time, error) { | ||
exp := time.Now().Add(time.Hour * 24) | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
"id": user.ID, | ||
"login": user.Login, | ||
"exp": exp.Unix(), | ||
}) | ||
tokenStr, err := token.SignedString([]byte(os.Getenv("JWT_SECRET"))) | ||
if err != nil { | ||
return "", time.Now(), err | ||
} | ||
return tokenStr, exp, nil | ||
} | ||
|
||
// ParseToken parses the provided JWT token string and returns the parsed token. | ||
func ParseToken(token string) (*jwt.Token, error) { | ||
return jwt.Parse(token, 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 []byte(os.Getenv("JWT_SECRET")), nil | ||
}) | ||
} | ||
|
||
// ParseId parses the user ID from the JWT token claims. | ||
func ParseId(claims *jwt.Token) (uuid.UUID, error) { | ||
payloadMap, ok := claims.Claims.(jwt.MapClaims) | ||
if !ok { | ||
return uuid.Nil, errors.New("invalid claims") | ||
} | ||
idStr, ok := payloadMap["id"].(string) | ||
if !ok { | ||
return uuid.Nil, errors.New("incorrect id") | ||
} | ||
id, err := uuid.FromString(idStr) | ||
if err != nil { | ||
return uuid.Nil, errors.New("incorrect id") | ||
} | ||
|
||
return id, nil | ||
} |
Oops, something went wrong.