-
Notifications
You must be signed in to change notification settings - Fork 0
/
claimSession.go
71 lines (56 loc) · 1.19 KB
/
claimSession.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package pogifyapi
import (
"errors"
"log"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
gonanoid "github.com/matoous/go-nanoid"
)
type sessionJwtClaims struct {
Session string `json:"session"`
jwt.StandardClaims
}
// StartSession ...
func (s *server) claimSession(c *gin.Context) {
var sessionCode string
sid, exists := c.Get("sessionID")
if !exists {
c.AbortWithError(500, errors.New("sessionID not set in context"))
return
}
sessionCode = sid.(string)
refreshToken, err := gonanoid.ID(64)
if err != nil {
c.AbortWithError(500, err)
return
}
val, err := s.redis.newSession(sessionCode, refreshToken)
if err != nil {
log.Print(err)
c.AbortWithError(500, err)
return
}
if val != 1 {
c.String(410, "code taken")
return
}
claims := sessionJwtClaims{
sessionCode,
jwt.StandardClaims{
ExpiresAt: time.Now().Unix() + 60*60,
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
tokenSign, err := token.SignedString(s.jwt.secret)
if err != nil {
c.AbortWithError(500, err)
return
}
c.JSON(200, gin.H{
"session": sessionCode,
"refreshToken": refreshToken,
"expiresIn": 60 * 60,
"token": tokenSign,
})
}