Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc3eb94d

Browse files
committed
reuse apikey generate for more places
1 parenta955690 commitc3eb94d

File tree

8 files changed

+25
-32
lines changed

8 files changed

+25
-32
lines changed

‎coderd/apikey/apikey.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,19 @@ func GenerateSecret(length int) (secret string, hashed []byte, err error) {
154154
iferr!=nil {
155155
return"",nil,err
156156
}
157-
hash:=hashSecret(secret)
157+
hash:=HashSecret(secret)
158158
returnsecret,hash[:],nil
159159
}
160160

161161
// ValidateHash compares a secret against an expected hashed secret.
162162
funcValidateHash(hashedSecret []byte,secretstring)bool {
163-
hash:=hashSecret(secret)
163+
hash:=HashSecret(secret)
164164
returnsubtle.ConstantTimeCompare(hashedSecret,hash[:])==1
165165
}
166166

167-
//hashSecret is the single function used to hash API key secrets.
167+
//HashSecret is the single function used to hash API key secrets.
168168
// Use this to ensure a consistent hashing algorithm.
169-
funchashSecret(secretstring) []byte {
169+
funcHashSecret(secretstring) []byte {
170170
hash:=sha256.Sum256([]byte(secret))
171171
returnhash[:]
172172
}

‎coderd/database/dbgen/dbgen.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/stretchr/testify/require"
2121
"golang.org/x/xerrors"
2222

23+
"github.com/coder/coder/v2/coderd/apikey"
2324
"github.com/coder/coder/v2/coderd/database"
2425
"github.com/coder/coder/v2/coderd/database/db2sdk"
2526
"github.com/coder/coder/v2/coderd/database/dbauthz"
@@ -980,9 +981,8 @@ func WorkspaceResourceMetadatums(t testing.TB, db database.Store, seed database.
980981
}
981982

982983
funcWorkspaceProxy(t testing.TB,db database.Store,orig database.WorkspaceProxy) (database.WorkspaceProxy,string) {
983-
secret,err:=cryptorand.HexString(64)
984+
secret,hashedSecret,err:=apikey.GenerateSecret(64)
984985
require.NoError(t,err,"generate secret")
985-
hashedSecret:=sha256.Sum256([]byte(secret))
986986

987987
proxy,err:=db.InsertWorkspaceProxy(genCtx, database.InsertWorkspaceProxyParams{
988988
ID:takeFirst(orig.ID,uuid.New()),

‎coderd/httpmw/apikey_test.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"golang.org/x/exp/slices"
2020
"golang.org/x/oauth2"
2121

22+
"github.com/coder/coder/v2/coderd/apikey"
2223
"github.com/coder/coder/v2/coderd/database"
2324
"github.com/coder/coder/v2/coderd/database/dbauthz"
2425
"github.com/coder/coder/v2/coderd/database/dbgen"
@@ -32,10 +33,10 @@ import (
3233
"github.com/coder/coder/v2/testutil"
3334
)
3435

35-
funcrandomAPIKeyParts() (idstring,secretstring) {
36+
funcrandomAPIKeyParts() (idstring,secretstring,hashedSecret []byte) {
3637
id,_=cryptorand.String(10)
37-
secret,_=cryptorand.String(22)
38-
returnid,secret
38+
secret,hashedSecret,_=apikey.GenerateSecret(22)
39+
returnid,secret,hashedSecret
3940
}
4041

4142
funcTestAPIKey(t*testing.T) {
@@ -171,10 +172,10 @@ func TestAPIKey(t *testing.T) {
171172
t.Run("NotFound",func(t*testing.T) {
172173
t.Parallel()
173174
var (
174-
db,_=dbtestutil.NewDB(t)
175-
id,secret=randomAPIKeyParts()
176-
r=httptest.NewRequest("GET","/",nil)
177-
rw=httptest.NewRecorder()
175+
db,_=dbtestutil.NewDB(t)
176+
id,secret,_=randomAPIKeyParts()
177+
r=httptest.NewRequest("GET","/",nil)
178+
rw=httptest.NewRecorder()
178179
)
179180
r.Header.Set(codersdk.SessionTokenHeader,fmt.Sprintf("%s-%s",id,secret))
180181

‎coderd/httpmw/authorize_test.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package httpmw_test
22

33
import (
44
"context"
5-
"crypto/sha256"
65
"fmt"
76
"net"
87
"net/http"
@@ -143,8 +142,7 @@ func TestExtractUserRoles(t *testing.T) {
143142

144143
funcaddUser(t*testing.T,db database.Store,roles...string) (database.User,string) {
145144
var (
146-
id,secret=randomAPIKeyParts()
147-
hashed=sha256.Sum256([]byte(secret))
145+
id,secret,hashed=randomAPIKeyParts()
148146
)
149147
ifroles==nil {
150148
roles= []string{}

‎coderd/httpmw/workspaceparam_test.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package httpmw_test
22

33
import (
44
"context"
5-
"crypto/sha256"
65
"encoding/json"
76
"fmt"
87
"net"
@@ -32,8 +31,7 @@ func TestWorkspaceParam(t *testing.T) {
3231

3332
setup:=func(db database.Store) (*http.Request, database.User) {
3433
var (
35-
id,secret=randomAPIKeyParts()
36-
hashed=sha256.Sum256([]byte(secret))
34+
id,secret,hashed=randomAPIKeyParts()
3735
)
3836
r:=httptest.NewRequest("GET","/",nil)
3937
r.Header.Set(codersdk.SessionTokenHeader,fmt.Sprintf("%s-%s",id,secret))

‎coderd/provisionerkey/provisionerkey.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package provisionerkey
22

33
import (
4-
"crypto/sha256"
54
"crypto/subtle"
65

76
"github.com/google/uuid"
87
"golang.org/x/xerrors"
98

9+
"github.com/coder/coder/v2/coderd/apikey"
1010
"github.com/coder/coder/v2/coderd/database"
1111
"github.com/coder/coder/v2/coderd/database/dbtime"
12-
"github.com/coder/coder/v2/cryptorand"
1312
)
1413

1514
const (
1615
secretLength=43
1716
)
1817

1918
funcNew(organizationID uuid.UUID,namestring,tagsmap[string]string) (database.InsertProvisionerKeyParams,string,error) {
20-
secret,err:=cryptorand.String(secretLength)
19+
secret,hashed,err:=apikey.GenerateSecret(secretLength)
2120
iferr!=nil {
2221
return database.InsertProvisionerKeyParams{},"",xerrors.Errorf("generate secret: %w",err)
2322
}
@@ -31,7 +30,7 @@ func New(organizationID uuid.UUID, name string, tags map[string]string) (databas
3130
CreatedAt:dbtime.Now(),
3231
OrganizationID:organizationID,
3332
Name:name,
34-
HashedSecret:HashSecret(secret),
33+
HashedSecret:hashed,
3534
Tags:tags,
3635
},secret,nil
3736
}
@@ -45,8 +44,7 @@ func Validate(token string) error {
4544
}
4645

4746
funcHashSecret(secretstring) []byte {
48-
h:=sha256.Sum256([]byte(secret))
49-
returnh[:]
47+
returnapikey.HashSecret(secret)
5048
}
5149

5250
funcCompare(a []byte,b []byte)bool {

‎enterprise/coderd/workspaceproxy.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package coderd
22

33
import (
44
"context"
5-
"crypto/sha256"
65
"database/sql"
76
"fmt"
87
"net/http"
@@ -16,6 +15,7 @@ import (
1615

1716
"cdr.dev/slog"
1817
agpl"github.com/coder/coder/v2/coderd"
18+
"github.com/coder/coder/v2/coderd/apikey"
1919
"github.com/coder/coder/v2/coderd/audit"
2020
"github.com/coder/coder/v2/coderd/database"
2121
"github.com/coder/coder/v2/coderd/database/db2sdk"
@@ -28,7 +28,6 @@ import (
2828
"github.com/coder/coder/v2/coderd/workspaceapps"
2929
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
3030
"github.com/coder/coder/v2/codersdk"
31-
"github.com/coder/coder/v2/cryptorand"
3231
"github.com/coder/coder/v2/enterprise/coderd/proxyhealth"
3332
"github.com/coder/coder/v2/enterprise/replicasync"
3433
"github.com/coder/coder/v2/enterprise/wsproxy/wsproxysdk"
@@ -934,11 +933,11 @@ func (api *API) reconnectingPTYSignedToken(rw http.ResponseWriter, r *http.Reque
934933
}
935934

936935
funcgenerateWorkspaceProxyToken(id uuid.UUID) (tokenstring,hashed []byte,errerror) {
937-
secret,err:=cryptorand.HexString(64)
936+
secret,hashedSecret,err:=apikey.GenerateSecret(64)
938937
iferr!=nil {
939938
return"",nil,xerrors.Errorf("generate token: %w",err)
940939
}
941-
hashedSecret:=sha256.Sum256([]byte(secret))
940+
942941
fullToken:=fmt.Sprintf("%s:%s",id,secret)
943942
returnfullToken,hashedSecret[:],nil
944943
}

‎enterprise/x/aibridgedserver/aibridgedserver_test.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package aibridgedserver_test
22

33
import (
44
"context"
5-
"crypto/sha256"
65
"database/sql"
76
"encoding/json"
87
"fmt"
@@ -21,6 +20,7 @@ import (
2120
"google.golang.org/protobuf/types/known/structpb"
2221
"google.golang.org/protobuf/types/known/timestamppb"
2322

23+
"github.com/coder/coder/v2/coderd/apikey"
2424
"github.com/coder/coder/v2/coderd/database"
2525
"github.com/coder/coder/v2/coderd/database/dbmock"
2626
"github.com/coder/coder/v2/coderd/database/dbtime"
@@ -138,9 +138,8 @@ func TestAuthorization(t *testing.T) {
138138
}
139139

140140
keyID,_:=cryptorand.String(10)
141-
keySecret,_:=cryptorand.String(22)
141+
keySecret,keySecretHashed,_:=apikey.GenerateSecret(22)
142142
token:=fmt.Sprintf("%s-%s",keyID,keySecret)
143-
keySecretHashed:=sha256.Sum256([]byte(keySecret))
144143
apiKey:= database.APIKey{
145144
ID:keyID,
146145
LifetimeSeconds:86400,// default in db

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp