@@ -10,7 +10,6 @@ import (
10
10
"time"
11
11
12
12
"github.com/google/uuid"
13
- "golang.org/x/crypto/argon2"
14
13
"golang.org/x/oauth2"
15
14
"golang.org/x/xerrors"
16
15
@@ -21,6 +20,7 @@ import (
21
20
"github.com/coder/coder/v2/coderd/httpapi"
22
21
"github.com/coder/coder/v2/coderd/httpmw"
23
22
"github.com/coder/coder/v2/coderd/rbac"
23
+ "github.com/coder/coder/v2/coderd/userpassword"
24
24
"github.com/coder/coder/v2/codersdk"
25
25
"github.com/coder/coder/v2/cryptorand"
26
26
)
@@ -111,20 +111,23 @@ func Tokens(db database.Store, defaultLifetime time.Duration) http.HandlerFunc {
111
111
112
112
func authorizationCodeGrant (ctx context.Context , db database.Store , app database.OAuth2ProviderApp , defaultLifetime time.Duration , clientSecret , code string ) (oauth2.Token , error ) {
113
113
// Validate the client secret.
114
- secretHash := Hash (clientSecret , app .ID )
114
+ secretHash , err := userpassword .Hash (clientSecret )
115
+ if err != nil {
116
+ return oauth2.Token {}, err
117
+ }
115
118
secret , err := db .GetOAuth2ProviderAppSecretByAppIDAndSecret (
116
119
//nolint:gocritic // Users cannot read secrets so we must use the system.
117
120
dbauthz .AsSystemRestricted (ctx ),
118
121
database.GetOAuth2ProviderAppSecretByAppIDAndSecretParams {
119
122
AppID : app .ID ,
120
- HashedSecret : secretHash [:] ,
123
+ HashedSecret : [] byte ( secretHash ) ,
121
124
})
122
125
if err != nil {
123
126
return oauth2.Token {}, err
124
127
}
125
128
126
129
// Validate the authorization code.
127
- codeHash := Hash (code , app . ID )
130
+ codeHash , err := userpassword . Hash (code )
128
131
if err != nil {
129
132
return oauth2.Token {}, err
130
133
}
@@ -133,7 +136,7 @@ func authorizationCodeGrant(ctx context.Context, db database.Store, app database
133
136
dbauthz .AsSystemRestricted (ctx ),
134
137
database.GetOAuth2ProviderAppCodeByAppIDAndSecretParams {
135
138
AppID : app .ID ,
136
- HashedSecret : codeHash [:] ,
139
+ HashedSecret : [] byte ( codeHash ) ,
137
140
})
138
141
if err != nil {
139
142
return oauth2.Token {}, err
@@ -208,12 +211,15 @@ func authorizationCodeGrant(ctx context.Context, db database.Store, app database
208
211
return xerrors .Errorf ("insert oauth2 access token: %w" , err )
209
212
}
210
213
211
- hashed := Hash (rawRefreshToken , app .ID )
214
+ refreshHash , err := userpassword .Hash (rawRefreshToken )
215
+ if err != nil {
216
+ return xerrors .Errorf ("hash oauth2 refresh token: %w" , err )
217
+ }
212
218
_ , err = tx .InsertOAuth2ProviderAppToken (ctx , database.InsertOAuth2ProviderAppTokenParams {
213
219
ID : uuid .New (),
214
220
CreatedAt : dbtime .Now (),
215
221
ExpiresAt : key .ExpiresAt ,
216
- RefreshHash : hashed [:] ,
222
+ RefreshHash : [] byte ( refreshHash ) ,
217
223
AppSecretID : secret .ID ,
218
224
APIKeyID : newKey .ID ,
219
225
})
@@ -234,12 +240,3 @@ func authorizationCodeGrant(ctx context.Context, db database.Store, app database
234
240
// Expiry: key.ExpiresAt,
235
241
}, nil
236
242
}
237
-
238
- /**
239
- * Hash uses argon2 to hash the secret using the ID as the salt.
240
- */
241
- func Hash (secret string , id uuid.UUID ) []byte {
242
- b := []byte (secret )
243
- // TODO: Expose iterations, memory, and threads as configuration values?
244
- return argon2 .IDKey (b , []byte (id .String ()), 1 , 64 * 1024 , 2 , uint32 (len (b )))
245
- }
0 commit comments