Skip to content

Commit 171ea94

Browse files
committed
Merge branch 'using_storage'
2 parents 6dfacac + a32f507 commit 171ea94

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

login/login.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package login
22

33
type Loginer interface {
4-
Login (user, password string) (string, string)
4+
Login(user, password string) (string, string)
55
}
66

77
type SocialLoginer interface {
8-
Login (token string) string
8+
Login(token string) string
99
}
10-

main.go

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package main
22

33
import (
4-
jwt_lib "github.com/dgrijalva/jwt-go"
5-
"github.com/gin-gonic/contrib/jwt"
4+
_"fmt"
5+
"github.com/dgrijalva/jwt-go"
6+
"github.com/fatih/structs"
67
"github.com/gin-gonic/gin"
78
"github.com/gophergala2016/golin/login"
89
"github.com/gophergala2016/golin/tokens"
@@ -22,29 +23,41 @@ type Token struct {
2223
Token string
2324
}
2425

26+
type Claim struct {
27+
Id string
28+
exp int64
29+
}
30+
2531
func (u User) Login(user, password string) (string, string) {
2632
if password == "qwerty" {
2733
return user, "[email protected]"
2834
}
2935
return "No", "NO"
3036
}
3137

32-
func (t Token) GenerateToken(SignatureStr string) string {
33-
var tokenStr string
38+
func (t Token) GenerateToken(SignatureStr string) (string, error) {
39+
var claim Claim
40+
41+
claim.Id = SignatureStr
42+
claim.exp = time.Now().Add(time.Hour * 1).Unix()
3443

35-
token := jwt_lib.New(jwt_lib.GetSigningMethod("HS256"))
36-
token.Claims["ID"] = SignatureStr
37-
token.Claims["exp"] = time.Now().Add(time.Hour * 1).Unix()
38-
tokenStr, _ = token.SignedString([]byte(secret))
44+
alg := jwt.GetSigningMethod("HS256")
45+
token := jwt.New(alg)
46+
token.Claims = structs.Map(claim)
3947

40-
return tokenStr
48+
if tokenStr, err := token.SignedString([]byte(secret)); err == nil {
49+
return tokenStr, nil
50+
} else {
51+
return "", err
52+
}
53+
//TODO Storage
4154
}
4255

4356
func main() {
4457
r := gin.Default()
4558
publics := r.Group("api/v1/public")
4659
privates := r.Group("api/v1/private")
47-
privates.Use(jwt.Auth(secret))
60+
//privates.Use(jwt.Auth(secret)) //TODO Change by verify method
4861
privates.GET("/users/:id", GetUser)
4962
publics.POST("/users", LoginUser)
5063
r.Run(":8080")
@@ -53,6 +66,7 @@ func main() {
5366
func LoginUser(c *gin.Context) {
5467
var user User
5568
var loginer login.Loginer
69+
var tokenStr string
5670

5771
var token Token
5872
var tokener tokens.Tokener
@@ -63,8 +77,13 @@ func LoginUser(c *gin.Context) {
6377
tokener = token
6478

6579
SignatureStr, email := loginer.Login(user.Username, user.Password)
66-
//token := tokens.GenerateToken(SignatureStr)
67-
c.JSON(201, gin.H{"token": tokener.GenerateToken(SignatureStr), "email": email})
80+
tokenStr, err := tokener.GenerateToken(SignatureStr)
81+
82+
if err != nil {
83+
c.JSON(404, gin.H{"error": err})
84+
} else {
85+
c.JSON(201, gin.H{"token": tokenStr, "email": email})
86+
}
6887

6988
}
7089

tokens/tokens.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tokens
22

33
type Tokener interface {
4-
GenerateToken(SignaturStr string) string
4+
GenerateToken(SignaturStr string) (string, error)
55
}
66

77
var secret = "ChAvO"

0 commit comments

Comments
 (0)