/
auth.go
107 lines (94 loc) · 3.13 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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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 auth provides methods to generate valid authorization values and
// to also decode them.
//
// It also provides methods to validate passwords.
package auth
import (
"context"
"crypto/rand"
"encoding/base32"
"fmt"
"strings"
"go.thethings.network/lorawan-stack/pkg/errors"
)
const (
idLength = 24
keyLength = 32
)
var enc = base32.StdEncoding.WithPadding(base32.NoPadding)
var (
// APIKey authenticates calls on behalf of an entity on itself, or of users or organizations.
APIKey = TokenType(enc.EncodeToString([]byte("key")))
// AccessToken authenticates calls on behalf of a user that authorized an OAuth client.
AccessToken = TokenType(enc.EncodeToString([]byte("acc")))
// RefreshToken is used by OAuth clients to refresh AccessTokens.
RefreshToken = TokenType(enc.EncodeToString([]byte("ref")))
// AuthorizationCode is used by OAuth clients to exchange AccessTokens.
AuthorizationCode = TokenType(enc.EncodeToString([]byte("aut")))
)
// TokenType indicates the type of a token.
type TokenType string
// Generate a token of this type.
// The ID is only generated if not already given.
func (t TokenType) Generate(ctx context.Context, id string) (token string, err error) {
if id == "" {
id, err = GenerateID(ctx)
if err != nil {
return "", err
}
}
key, err := GenerateKey(ctx)
if err != nil {
return "", err
}
return JoinToken(t, id, key), nil
}
var errInvalidToken = errors.DefineInvalidArgument("token", "invalid token")
// SplitToken splits the token from "<prefix>.<id>.<key>".
func SplitToken(token string) (tokenType TokenType, id, key string, err error) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return "", "", "", errInvalidToken.New()
}
switch TokenType(parts[0]) {
case APIKey, AccessToken, RefreshToken, AuthorizationCode:
return TokenType(parts[0]), parts[1], parts[2], nil
default:
return "", "", "", errInvalidToken.New()
}
}
// JoinToken joins the token as "<prefix>.<id>.<key>".
func JoinToken(tokenType TokenType, id, key string) string {
return fmt.Sprintf("%s.%s.%s", string(tokenType), id, key)
}
// GenerateID generates the "id" part of the token.
func GenerateID(_ context.Context) (string, error) {
var b [idLength]byte
_, err := rand.Read(b[:])
if err != nil {
return "", err
}
return enc.EncodeToString(b[:]), nil
}
// GenerateKey generates the "key" part of the token.
func GenerateKey(_ context.Context) (string, error) {
var b [keyLength]byte
_, err := rand.Read(b[:])
if err != nil {
return "", err
}
return enc.EncodeToString(b[:]), nil
}