/
token.go
185 lines (156 loc) · 4.96 KB
/
token.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
/*-
Copyright (c) 2016, Jörg Pernfuß <code.jpe@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package auth
import (
"bytes"
"crypto/rand"
"encoding/hex"
"net"
"net/http"
"time"
"github.com/mjolnir42/scrypth64"
)
// Token is the data passed between client and server to
// authenticate the client and issue a token for it that can be used
// as HTTP Basic Auth password.
type Token struct {
UserName string `json:"username"`
Password string `json:"password,omitempty"`
Token string `json:"token,omitempty"`
ValidFrom string `json:"validFrom,omitempty"`
ExpiresAt string `json:"expiresAt,omitempty"`
Salt string `json:"-"`
SourceIP net.IP `json:"-"`
}
// NewToken returns an empty Token
func NewToken() *Token {
return &Token{}
}
// Generate verifies a the embedded credentials in Token and
// issues a new token to be returned to the user. Calling
// GenerateToken consumes the embedded password regardless of outcome.
// Returns ErrAuth if the password is incorrect.
func (t *Token) Generate(mcf scrypth64.Mcf, key, seed []byte) error {
var (
err error
ok bool
stuntSeed, stuntKey []byte
)
// do not generate Tokens with an incorrectly configured seed or
// key
if seed == nil || key == nil || len(seed) == 0 || len(key) == 0 {
err = ErrInput
goto fail
}
// net.IP is a bit of a pain since an unset net.IP does not equal
// nil, and .String() prints `<nil>`
if t.SourceIP.Equal(net.IP{}) {
err = ErrInput
goto fail
}
// Username and password have to be set
if t.UserName == "" || t.Password == "" {
err = ErrInput
goto fail
}
// stunt data
stuntSeed = bytes.Repeat([]byte{0x0F}, len(seed))
stuntKey = bytes.Repeat([]byte{0xAB}, len(key))
// start of timing critical path
if ok, err = t.comparePassword(mcf); !ok || err != nil {
// spend some quality time computing garbage
t.mixToken(stuntKey, stuntSeed)
goto fail
}
if err = t.mixToken(key, seed); err != nil {
goto fail
}
t.zeroPassword()
return nil
fail:
t.zeroPassword()
if err != nil {
return err
}
return ErrAuth
}
// zeroPassword ensures the Password field is set to the zero value
func (t *Token) zeroPassword() {
t.Password = ""
}
// comparePassword is used to verify the user supplied password
// against a scrypth64.Mcf
func (t *Token) comparePassword(mcf scrypth64.Mcf) (bool, error) {
return scrypth64.Verify(t.Password, mcf)
}
// mixToken generates a new password token
func (t *Token) mixToken(key, seed []byte) error {
var (
salt, bin, btime []byte
err error
valid, expires time.Time
)
// expiry time
valid = time.Now().UTC()
expires = valid.Add(
time.Duration(TokenExpirySeconds) * time.Second,
).UTC()
if btime, err = expires.MarshalBinary(); err != nil {
goto fail
}
t.ValidFrom = valid.Format(rfc3339Milli)
t.ExpiresAt = expires.Format(rfc3339Milli)
// whiteout unstable subsecond timestamp part with "random" value
copy(btime[9:], []byte{0xde, 0xad, 0xca, 0xfe})
// add random salt
salt = make([]byte, 16)
if _, err = rand.Read(salt); err != nil {
goto fail
}
t.Salt = hex.EncodeToString(salt)
// compute the token
bin = computeToken(
[]byte(t.UserName),
key,
seed,
btime,
salt,
[]byte(t.SourceIP.String()),
)
t.Token = hex.EncodeToString(bin)
return nil
fail:
return err
}
// SetIPAddress records the client's IP address
func (t *Token) SetIPAddress(r *http.Request) {
t.SetIPAddressString(r.RemoteAddr)
}
// SetIPAddressString records the client's IP address
func (t *Token) SetIPAddressString(addr string) {
t.SetIPAddressExtractedString(extractAddress(addr))
}
// SetIPAddressExtractedString records the client's IP address
func (t *Token) SetIPAddressExtractedString(addr string) {
t.SourceIP = net.ParseIP(addr)
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix