-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
307 lines (269 loc) · 7.58 KB
/
auth.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package component
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/url"
"path"
"path/filepath"
"strings"
"time"
"github.com/TheThingsNetwork/go-account-lib/cache"
"github.com/TheThingsNetwork/go-account-lib/claims"
"github.com/TheThingsNetwork/go-account-lib/keys"
"github.com/TheThingsNetwork/go-account-lib/oauth"
"github.com/TheThingsNetwork/go-account-lib/tokenkey"
"github.com/TheThingsNetwork/ttn/api"
pb_discovery "github.com/TheThingsNetwork/ttn/api/discovery"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/TheThingsNetwork/ttn/utils/security"
jwt "github.com/dgrijalva/jwt-go"
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
)
// InitAuth initializes Auth functionality
func (c *Component) InitAuth() error {
inits := []func() error{
c.initAuthServers,
c.initKeyPair,
c.initRoots,
}
if c.Config.UseTLS {
inits = append(inits, c.initTLS)
}
for _, init := range inits {
if err := init(); err != nil {
return err
}
}
return nil
}
type authServer struct {
url string
username string
password string
}
func parseAuthServer(str string) (srv authServer, err error) {
url, err := url.Parse(str)
if err != nil {
return srv, err
}
srv.url = fmt.Sprintf("%s://%s", url.Scheme, url.Host)
if url.User != nil {
srv.username = url.User.Username()
srv.password, _ = url.User.Password()
}
return srv, nil
}
func (c *Component) initAuthServers() error {
urlMap := make(map[string]string)
funcMap := make(map[string]tokenkey.TokenFunc)
var httpProvider tokenkey.Provider
for id, url := range c.Config.AuthServers {
id, url := id, url // deliberately shadow these
if strings.HasPrefix(url, "file://") {
file := strings.TrimPrefix(url, "file://")
contents, err := ioutil.ReadFile(path.Clean(file))
if err != nil {
return err
}
funcMap[id] = func(renew bool) (*tokenkey.TokenKey, error) {
return &tokenkey.TokenKey{Algorithm: "ES256", Key: string(contents)}, nil
}
continue
}
srv, err := parseAuthServer(url)
if err != nil {
return err
}
urlMap[id] = srv.url
funcMap[id] = func(renew bool) (*tokenkey.TokenKey, error) {
return httpProvider.Get(id, renew)
}
}
httpProvider = tokenkey.HTTPProvider(
urlMap,
cache.WriteTroughCacheWithFormat(c.Config.KeyDir, "auth-%s.pub"),
)
c.TokenKeyProvider = tokenkey.FuncProvider(funcMap)
return nil
}
// UpdateTokenKey updates the OAuth Bearer token key
func (c *Component) UpdateTokenKey() error {
if c.TokenKeyProvider == nil {
return errors.NewErrInternal("No public key provider configured for token validation")
}
// Set up Auth Server Token Validation
err := c.TokenKeyProvider.Update()
if err != nil {
c.Ctx.Warnf("ttn: Failed to refresh public keys for token validation: %s", err.Error())
} else {
c.Ctx.Info("ttn: Got public keys for token validation")
}
return nil
}
func (c *Component) initKeyPair() error {
priv, err := security.LoadKeypair(c.Config.KeyDir)
if err != nil {
return err
}
c.privateKey = priv
pubPEM, _ := security.PublicPEM(priv)
c.Identity.PublicKey = string(pubPEM)
return nil
}
func (c *Component) initTLS() error {
cert, err := security.LoadCert(c.Config.KeyDir)
if err != nil {
return err
}
c.Identity.Certificate = string(cert)
privPEM, _ := security.PrivatePEM(c.privateKey)
cer, err := tls.X509KeyPair(cert, privPEM)
if err != nil {
return err
}
c.tlsConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
return nil
}
func (c *Component) initRoots() error {
path := filepath.Clean(c.Config.KeyDir + "/ca.cert")
cert, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
if !api.RootCAs.AppendCertsFromPEM(cert) {
return fmt.Errorf("Could not add root certificates from %s", path)
}
return nil
}
// BuildJWT builds a short-lived JSON Web Token for this component
func (c *Component) BuildJWT() (string, error) {
if c.privateKey != nil {
privPEM, err := security.PrivatePEM(c.privateKey)
if err != nil {
return "", err
}
return security.BuildJWT(c.Identity.Id, 20*time.Second, privPEM)
}
return "", nil
}
// GetContext returns a context for outgoing RPC request. If token is "", this function will generate a short lived token from the component
func (c *Component) GetContext(token string) context.Context {
var serviceName, serviceVersion, id, netAddress string
if c.Identity != nil {
serviceName = c.Identity.ServiceName
id = c.Identity.Id
if token == "" {
token, _ = c.BuildJWT()
}
serviceVersion = c.Identity.ServiceVersion
netAddress = c.Identity.NetAddress
}
md := metadata.Pairs(
"service-name", serviceName,
"service-version", serviceVersion,
"id", id,
"token", token,
"net-address", netAddress,
)
ctx := metadata.NewContext(context.Background(), md)
return ctx
}
var oauthCache = cache.MemoryCache()
// ExchangeAppKeyForToken enables authentication with the App Access Key
func (c *Component) ExchangeAppKeyForToken(appID, key string) (string, error) {
issuerID := keys.KeyIssuer(key)
if issuerID == "" {
// Take the first configured auth server
for k := range c.Config.AuthServers {
issuerID = k
break
}
key = fmt.Sprintf("%s.%s", issuerID, key)
}
issuer, ok := c.Config.AuthServers[issuerID]
if !ok {
return "", fmt.Errorf("Auth server %s not registered", issuer)
}
srv, _ := parseAuthServer(issuer)
oauth := oauth.NewWithCache(srv.url, &oauth.Client{
ID: srv.username,
Secret: srv.password,
}, oauthCache)
token, err := oauth.ExchangeAppKeyForToken(appID, key)
if err != nil {
return "", err
}
return token.AccessToken, nil
}
// ValidateNetworkContext validates the context of a network request (router-broker, broker-handler, etc)
func (c *Component) ValidateNetworkContext(ctx context.Context) (component *pb_discovery.Announcement, err error) {
defer func() {
if err != nil {
time.Sleep(time.Second)
}
}()
md, ok := metadata.FromContext(ctx)
if !ok {
err = errors.NewErrInternal("Could not get metadata from context")
return
}
var id, serviceName, token string
if ids, ok := md["id"]; ok && len(ids) == 1 {
id = ids[0]
}
if id == "" {
err = errors.NewErrInvalidArgument("Metadata", "id missing")
return
}
if serviceNames, ok := md["service-name"]; ok && len(serviceNames) == 1 {
serviceName = serviceNames[0]
}
if serviceName == "" {
err = errors.NewErrInvalidArgument("Metadata", "service-name missing")
return
}
if tokens, ok := md["token"]; ok && len(tokens) == 1 {
token = tokens[0]
}
var announcement *pb_discovery.Announcement
announcement, err = c.Discover(serviceName, id)
if err != nil {
return
}
if announcement.PublicKey == "" {
return announcement, nil
}
if token == "" {
err = errors.NewErrInvalidArgument("Metadata", "token missing")
return
}
var claims *jwt.StandardClaims
claims, err = security.ValidateJWT(token, []byte(announcement.PublicKey))
if err != nil {
return
}
if claims.Issuer != id {
err = errors.NewErrInvalidArgument("Metadata", "token was issued by different component id")
return
}
return announcement, nil
}
// ValidateTTNAuthContext gets a token from the context and validates it
func (c *Component) ValidateTTNAuthContext(ctx context.Context) (*claims.Claims, error) {
token, err := api.TokenFromContext(ctx)
if err != nil {
return nil, err
}
if c.TokenKeyProvider == nil {
return nil, errors.NewErrInternal("No token provider configured")
}
claims, err := claims.FromToken(c.TokenKeyProvider, token)
if err != nil {
return nil, errors.NewErrPermissionDenied(err.Error())
}
return claims, nil
}