forked from corestoreio/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
170 lines (149 loc) · 5.48 KB
/
service.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jwt
import (
"github.com/corestoreio/errors"
"github.com/corestoreio/log"
"github.com/corestoreio/pkg/store/scope"
"github.com/corestoreio/pkg/util/csjwt"
)
//go:generate go run ../internal/scopedservice/main_copy.go "$GOPACKAGE"
const (
claimExpiresAt = "exp"
claimIssuedAt = "iat"
claimKeyID = "jti"
)
// Service main type for handling JWT authentication, generation, blockLists and
// log outs depending on a scope.
type Service struct {
service
// JTI generates for each token a new ID. It panics on nil. Default ID
// generator runs in package "github.com/teris-io/shortid".
JTI IDGenerator
// Blocklist concurrent safe black list service which handles blocked
// tokens. Default black hole storage. Must be thread safe.
Blocklist Blocklister
}
// New creates a new token service.
// Default values from option function WithDefaultConfig() will be
// applied if passing no functional option arguments.
func New(opts ...Option) (*Service, error) {
s, err := newService(opts...)
if err != nil {
return nil, err
}
s.useWebsite = true
s.optionAfterApply = func() error {
s.rwmu.RLock()
defer s.rwmu.RUnlock()
for h := range s.scopeCache {
// This one checks if the configuration contains only the default or
// website scope. Store scope is neither allowed nor supported.
if scp, _ := h.Unpack(); scp > scope.Website {
return errors.NewNotSupportedf(errServiceUnsupportedScope, h)
}
}
return nil
}
if s.JTI == nil {
s.JTI = jti{}
}
if s.Blocklist == nil {
s.Blocklist = nullBL{}
}
if err := s.optionAfterApply(); err != nil {
return nil, err
}
return s, nil
}
// NewToken creates a new signed JSON web token based on the predefined scoped
// based template token function (WithTemplateToken) and merges the optional 3rd
// argument into the template token claim. The returned token is owned by the
// caller. The tokens Raw field contains the freshly signed byte slice.
// ExpiresAt, IssuedAt and ID are already set and cannot be overwritten, but you
// can access them. It panics if the provided template token has a nil Header or
// Claimer field.
func (s *Service) NewToken(scopeID scope.TypeID, claim ...csjwt.Claimer) (csjwt.Token, error) {
var empty csjwt.Token
now := csjwt.TimeFunc()
sc, err := s.ConfigByScopeID(scopeID, 0)
if err != nil {
return empty, errors.Wrap(err, "[jwt] NewToken.ConfigByScopeID")
}
tk := sc.TemplateToken()
if len(claim) > 0 && claim[0] != nil {
if err := csjwt.MergeClaims(tk.Claims, claim...); err != nil {
return empty, errors.Wrap(err, "[jwt] NewToken.MergeClaims")
}
}
if err := tk.Claims.Set(claimExpiresAt, now.Add(sc.Expire).Unix()); err != nil {
return empty, errors.Wrap(err, "[jwt] NewToken.Claims.Set EXP")
}
if err := tk.Claims.Set(claimIssuedAt, now.Unix()); err != nil {
return empty, errors.Wrap(err, "[jwt] NewToken.Claims.Set IAT")
}
jti, err := s.JTI.NewID()
if err != nil {
return empty, errors.Wrap(err, "[jwt] NewToken.Claims.Set JTI.NewID")
}
if err := tk.Claims.Set(claimKeyID, jti); err != nil {
return empty, errors.Wrapf(err, "[jwt] NewToken.Claims.Set KID: %q", jti)
}
tk.Raw, err = tk.SignedString(sc.SigningMethod, sc.Key)
return tk, errors.Wrap(err, "[jwt] NewToken.SignedString")
}
// Logout adds a token securely to a blockList with the expiration duration. If
// the JTI or token ID is empty or missing, an error gets returned of behaviour
// Empty.
func (s *Service) Logout(token csjwt.Token) error {
if len(token.Raw) == 0 || !token.Valid {
return nil
}
kid, err := extractJTI(token)
if err != nil {
return errors.Wrap(err, "[jwt] Service.Logout extractJTI")
}
return errors.Wrap(s.Blocklist.Set(kid, token.Claims.Expires()), "[jwt] Service.Logout.Blocklist.Set")
}
// Parse parses a token string with the DefaultID scope and returns the
// valid token or an error.
func (s *Service) Parse(rawToken []byte) (csjwt.Token, error) {
return s.ParseScoped(scope.DefaultTypeID, rawToken)
}
// ParseScoped parses a token based on the applied scope and the scope ID.
// Different configurations are passed to the token parsing function. The black
// list will be checked for containing entries.
func (s *Service) ParseScoped(scopeID scope.TypeID, rawToken []byte) (csjwt.Token, error) {
var empty csjwt.Token
sc, err := s.ConfigByScopeID(scopeID, 0)
if err != nil {
return empty, errors.Wrap(err, "[jwt] ParseScoped.ConfigByScopeID")
}
token, err := sc.Parse(rawToken)
if err != nil {
return empty, errors.Wrap(err, "[jwt] ParseScoped.Parse")
}
var inBL bool
isValid := token.Valid && len(token.Raw) > 0
if isValid {
inBL = s.Blocklist.Has(token.Raw)
}
if isValid && !inBL {
return token, nil
}
if s.Log.IsDebug() {
s.Log.Debug("jwt.Service.ParseScoped", log.Err(err), log.Bool("block_listed", inBL), log.String("rawToken", string(rawToken)), log.Marshal("token", token))
}
return empty, errors.NewNotValidf(errTokenParseNotValidOrBlockListed)
}