forked from textileio/go-textile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.go
142 lines (127 loc) · 3.38 KB
/
tokens.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package cafe
import (
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/textileio/textile-go/cafe/auth"
"github.com/textileio/textile-go/cafe/models"
"github.com/textileio/textile-go/net/service"
"io/ioutil"
"net/http"
"strings"
"time"
)
var errForbidden = "forbidden"
var forbiddenResponse = models.Response{
Status: http.StatusForbidden,
Error: &errForbidden,
}
func (c *Cafe) verify(token *jwt.Token) (interface{}, error) {
return []byte(c.TokenSecret), nil
}
func (c *Cafe) auth(g *gin.Context) {
if g.Request.URL.Path == "/api/v0/users" || g.Request.URL.Path == "/api/v0/referrals" {
return
}
// extract token string from request header
var tokenString string
parsed := strings.Split(g.Request.Header.Get("Authorization"), " ")
if len(parsed) == 2 {
tokenString = parsed[1]
}
// parse it
token, pErr := jwt.Parse(tokenString, c.verify)
// pull out claims
claims, err := auth.ParseClaims(token.Claims)
if err != nil {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
// check valid
if pErr != nil {
if !claims.VerifyExpiresAt(time.Now().Unix(), true) {
// 401 indicates a retry is expected after a token refresh
g.JSON(http.StatusUnauthorized, models.Response{
Status: http.StatusUnauthorized,
})
return
}
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
// check scope
tokenRoute := strings.Contains(g.Request.URL.Path, "/tokens")
switch claims.Scope {
case auth.Access:
if tokenRoute {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
case auth.Refresh:
if !tokenRoute {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
default:
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
// verify extra fields
if !claims.VerifyAudience(string(service.TextileProtocol), true) {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
}
func (c *Cafe) refreshToken(g *gin.Context) {
body, err := ioutil.ReadAll(g.Request.Body)
if err != nil {
g.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
accessToken := string(body)
// ensure bearer matches payload refresh token
var refreshToken string
parsed := strings.Split(g.Request.Header.Get("Authorization"), " ")
if len(parsed) == 2 {
refreshToken = parsed[1]
}
// ensure access and token are a valid pair
access, _ := jwt.Parse(accessToken, c.verify)
if access == nil {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
refresh, _ := jwt.Parse(refreshToken, c.verify)
if refresh == nil {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
accessClaims, err := auth.ParseClaims(access.Claims)
if err != nil {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
refreshClaims, err := auth.ParseClaims(refresh.Claims)
if err != nil {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
if refreshClaims.Id[1:] != accessClaims.Id {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
if refreshClaims.Subject != accessClaims.Subject {
g.JSON(http.StatusForbidden, forbiddenResponse)
return
}
// get a new session
refreshed, err := auth.NewSession(accessClaims.Subject, c.TokenSecret, c.Ipfs().Identity.Pretty(), service.TextileProtocol, month)
if err != nil {
g.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// ship it
g.JSON(http.StatusOK, models.Response{
Status: http.StatusOK,
Session: refreshed,
})
}