Skip to content

Commit

Permalink
OIDC cookie: use a cryptographically secure random string
Browse files Browse the repository at this point in the history
Signed-off-by: Nicola Murino <[email protected]>
  • Loading branch information
drakkan committed Nov 21, 2024
1 parent ed5ff9c commit f30a9a2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
6 changes: 1 addition & 5 deletions internal/httpd/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package httpd

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"sync"
Expand Down Expand Up @@ -53,10 +51,8 @@ type oauth2PendingAuth struct {
}

func newOAuth2PendingAuth(provider int, redirectURL, clientID string, clientSecret *kms.Secret) oauth2PendingAuth {
state := sha256.Sum256(util.GenerateRandomBytes(32))

return oauth2PendingAuth{
State: hex.EncodeToString(state[:]),
State: util.GenerateOpaqueString(),
Provider: provider,
ClientID: clientID,
ClientSecret: clientSecret,
Expand Down
11 changes: 3 additions & 8 deletions internal/httpd/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ package httpd

import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -204,12 +202,9 @@ type oidcPendingAuth struct {
}

func newOIDCPendingAuth(audience tokenAudience) oidcPendingAuth {
state := sha256.Sum256(util.GenerateRandomBytes(32))
nonce := util.GenerateUniqueID()

return oidcPendingAuth{
State: hex.EncodeToString(state[:]),
Nonce: nonce,
State: util.GenerateOpaqueString(),
Nonce: util.GenerateOpaqueString(),
Audience: audience,
IssuedAt: util.GetTimeAsMsSinceEpoch(time.Now()),
}
Expand Down Expand Up @@ -684,7 +679,7 @@ func (s *httpdServer) handleOIDCRedirect(w http.ResponseWriter, r *http.Request)
RefreshToken: oauth2Token.RefreshToken,
IDToken: rawIDToken,
Nonce: idToken.Nonce,
Cookie: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
}
if !oauth2Token.Expiry.IsZero() {
token.ExpiresAt = util.GetTimeAsMsSinceEpoch(oauth2Token.Expiry)
Expand Down
22 changes: 11 additions & 11 deletions internal/httpd/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func TestOIDCLoginLogout(t *testing.T) {
assert.Contains(t, rr.Body.String(), util.I18nInvalidAuth)

expiredAuthReq := oidcPendingAuth{
State: xid.New().String(),
Nonce: xid.New().String(),
State: util.GenerateOpaqueString(),
Nonce: util.GenerateOpaqueString(),
Audience: tokenAudienceWebClient,
IssuedAt: util.GetTimeAsMsSinceEpoch(time.Now().Add(-10 * time.Minute)),
}
Expand Down Expand Up @@ -564,7 +564,7 @@ func TestOIDCRefreshToken(t *testing.T) {
r, err := http.NewRequest(http.MethodGet, webUsersPath, nil)
assert.NoError(t, err)
token := oidcToken{
Cookie: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
AccessToken: xid.New().String(),
TokenType: "Bearer",
ExpiresAt: util.GetTimeAsMsSinceEpoch(time.Now().Add(-1 * time.Minute)),
Expand Down Expand Up @@ -668,7 +668,7 @@ func TestOIDCRefreshToken(t *testing.T) {

func TestOIDCRefreshUser(t *testing.T) {
token := oidcToken{
Cookie: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
AccessToken: xid.New().String(),
TokenType: "Bearer",
ExpiresAt: util.GetTimeAsMsSinceEpoch(time.Now().Add(1 * time.Minute)),
Expand Down Expand Up @@ -782,7 +782,7 @@ func TestValidateOIDCToken(t *testing.T) {
},
}
token := oidcToken{
Cookie: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
AccessToken: xid.New().String(),
ExpiresAt: util.GetTimeAsMsSinceEpoch(time.Now().Add(-2 * time.Minute)),
}
Expand All @@ -798,8 +798,8 @@ func TestValidateOIDCToken(t *testing.T) {

server.tokenAuth = jwtauth.New("PS256", util.GenerateRandomBytes(32), nil)
token = oidcToken{
Cookie: xid.New().String(),
AccessToken: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
AccessToken: util.GenerateUniqueID(),
}
oidcMgr.addToken(token)
rr = httptest.NewRecorder()
Expand All @@ -813,7 +813,7 @@ func TestValidateOIDCToken(t *testing.T) {
assert.Len(t, oidcMgr.tokens, 0)

token = oidcToken{
Cookie: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
AccessToken: xid.New().String(),
Role: "admin",
}
Expand Down Expand Up @@ -1107,7 +1107,7 @@ func TestMemoryOIDCManager(t *testing.T) {
AccessToken: xid.New().String(),
Nonce: xid.New().String(),
SessionID: xid.New().String(),
Cookie: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
Username: xid.New().String(),
Role: "admin",
Permissions: []string{dataprovider.PermAdminAny},
Expand Down Expand Up @@ -1157,7 +1157,7 @@ func TestMemoryOIDCManager(t *testing.T) {
token.UsedAt = usedAt
oidcMgr.tokens[token.Cookie] = token
newToken := oidcToken{
Cookie: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
}
oidcMgr.addToken(newToken)
oidcMgr.cleanup()
Expand Down Expand Up @@ -1663,7 +1663,7 @@ func TestDbOIDCManager(t *testing.T) {
}

token := oidcToken{
Cookie: xid.New().String(),
Cookie: util.GenerateOpaqueString(),
AccessToken: xid.New().String(),
TokenType: "Bearer",
RefreshToken: xid.New().String(),
Expand Down
10 changes: 9 additions & 1 deletion internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"encoding/json"
"encoding/pem"
"errors"
Expand Down Expand Up @@ -550,7 +552,7 @@ func createDirPathIfMissing(file string, perm os.FileMode) error {
return nil
}

// GenerateRandomBytes generates the secret to use for JWT auth
// GenerateRandomBytes generates random bytes with the specified length
func GenerateRandomBytes(length int) []byte {
b := make([]byte, length)
_, err := io.ReadFull(rand.Reader, b)
Expand All @@ -560,6 +562,12 @@ func GenerateRandomBytes(length int) []byte {
return b
}

// GenerateOpaqueString generates a cryptographically secure opaque string
func GenerateOpaqueString() string {
randomBytes := sha256.Sum256(GenerateRandomBytes(32))
return hex.EncodeToString(randomBytes[:])
}

// GenerateUniqueID returns an unique ID
func GenerateUniqueID() string {
u, err := uuid.NewRandom()
Expand Down

0 comments on commit f30a9a2

Please sign in to comment.