Skip to content

Commit 95744b4

Browse files
author
raul-brainattica
committed
User UUID added as Claim to token
1 parent 10d2bd5 commit 95744b4

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

controllers/auth_controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ func Login(w http.ResponseWriter, r *http.Request) {
1919
}
2020

2121
func RefresfhToken(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
22+
requestUser := new(models.User)
23+
decoder := json.NewDecoder(r.Body)
24+
decoder.Decode(&requestUser)
25+
2226
w.Header().Set("Content-Type", "application/json")
23-
w.Write(services.RefreshToken())
27+
w.Write(services.RefreshToken(requestUser))
2428
}
2529

2630
func Logout(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {

core/authentication/jwt_backend.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package authentication
33
import (
44
"api.jwt.auth/core/redis"
55
"api.jwt.auth/services/models"
6+
"code.google.com/p/go-uuid/uuid"
67
jwt "github.com/dgrijalva/jwt-go"
78
"golang.org/x/crypto/bcrypt"
89
"io/ioutil"
@@ -30,10 +31,11 @@ func InitJWTAuthenticationBackend() *JWTAuthenticationBackend {
3031
return authBack
3132
}
3233

33-
func (backend *JWTAuthenticationBackend) GenerateToken() string {
34+
func (backend *JWTAuthenticationBackend) GenerateToken(user *models.User) string {
3435
token := jwt.New(jwt.GetSigningMethod("RS256"))
3536
token.Claims["exp"] = time.Now().Add(time.Hour * time.Duration(tokenDuration)).Unix()
3637
token.Claims["iat"] = time.Now().Unix()
38+
token.Claims["sub"] = user.UUID
3739
tokenString, _ := token.SignedString(backend.privateKey)
3840
return tokenString
3941
}
@@ -42,6 +44,7 @@ func (backend *JWTAuthenticationBackend) Authenticate(user *models.User) bool {
4244
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("testing"), 10)
4345

4446
testUser := models.User{
47+
UUID: uuid.New(),
4548
Username: "haku",
4649
Password: string(hashedPassword),
4750
}

services/auth_service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ func Login(requestUser *models.User) (int, []byte) {
1313
authBackend := authentication.InitJWTAuthenticationBackend()
1414

1515
if authBackend.Authenticate(requestUser) {
16-
token := parameters.TokenAuthentication{authBackend.GenerateToken()}
16+
token := parameters.TokenAuthentication{authBackend.GenerateToken(requestUser)}
1717
response, _ := json.Marshal(token)
1818
return http.StatusOK, response
1919
}
2020

2121
return http.StatusUnauthorized, []byte("")
2222
}
2323

24-
func RefreshToken() []byte {
24+
func RefreshToken(requestUser *models.User) []byte {
2525
authBackend := authentication.InitJWTAuthenticationBackend()
26-
token := parameters.TokenAuthentication{authBackend.GenerateToken()}
26+
token := parameters.TokenAuthentication{authBackend.GenerateToken(requestUser)}
2727
response, err := json.Marshal(token)
2828
if err != nil {
2929
panic(err)

services/models/users.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
type User struct {
4+
UUID string `json:"uuid" form:"-"`
45
Username string `json:"username" form:"username""`
56
Password string `json:"password" form:"password"`
67
}

0 commit comments

Comments
 (0)