Skip to content

Commit

Permalink
Refactor Generate Token
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-brainattica committed Apr 16, 2015
1 parent 77b2a36 commit 4f0829a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
14 changes: 9 additions & 5 deletions core/authentication/jwt_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ func InitJWTAuthenticationBackend() *JWTAuthenticationBackend {
return authBackendInstance
}

func (backend *JWTAuthenticationBackend) GenerateToken(user *models.User) string {
func (backend *JWTAuthenticationBackend) GenerateToken(userUUID string) (string, error) {
token := jwt.New(jwt.GetSigningMethod("RS256"))
token.Claims["exp"] = time.Now().Add(time.Hour * time.Duration(tokenDuration)).Unix()
token.Claims["exp"] = time.Now().Add(time.Hour * time.Duration(settings.Get().JWTExpirationDelta)).Unix()
token.Claims["iat"] = time.Now().Unix()
token.Claims["sub"] = user.UUID
tokenString, _ := token.SignedString(backend.privateKey)
return tokenString
token.Claims["sub"] = userUUID
tokenString, err := token.SignedString(backend.privateKey)
if err != nil {
panic(err)
return "", err
}
return tokenString, nil
}

func (backend *JWTAuthenticationBackend) Authenticate(user *models.User) bool {
Expand Down
17 changes: 12 additions & 5 deletions services/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@ func Login(requestUser *models.User) (int, []byte) {
authBackend := authentication.InitJWTAuthenticationBackend()

if authBackend.Authenticate(requestUser) {
token := parameters.TokenAuthentication{authBackend.GenerateToken(requestUser)}
response, _ := json.Marshal(token)
return http.StatusOK, response
token, err := authBackend.GenerateToken(requestUser.UUID)
if err != nil {
return http.StatusInternalServerError, []byte("")
} else {
response, _ := json.Marshal(parameters.TokenAuthentication{token})
return http.StatusOK, response
}
}

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

func RefreshToken(requestUser *models.User) []byte {
authBackend := authentication.InitJWTAuthenticationBackend()
token := parameters.TokenAuthentication{authBackend.GenerateToken(requestUser)}
response, err := json.Marshal(token)
token, err := authBackend.GenerateToken(requestUser.UUID)
if err != nil {
panic(err)
}
response, err := json.Marshal(parameters.TokenAuthentication{token})
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion settings/pre.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"PrivateKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/private_key",
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub",
"JWTExpirationDelta": 72
}
3 changes: 2 additions & 1 deletion settings/prod.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"PrivateKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/private_key",
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub",
"JWTExpirationDelta": 72
}
5 changes: 3 additions & 2 deletions settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ var environments = map[string]string{
}

type Settings struct {
PrivateKeyPath string
PublicKeyPath string
PrivateKeyPath string
PublicKeyPath string
JWTExpirationDelta int
}

var settings Settings = Settings{}
Expand Down
3 changes: 2 additions & 1 deletion settings/tests.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"PrivateKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/private_key",
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub"
"PublicKeyPath": "/home/vagrant/go/src/api.jwt.auth/settings/keys/public_key.pub",
"JWTExpirationDelta": 72
}

0 comments on commit 4f0829a

Please sign in to comment.