Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch API keys from base64url to base32. #143

Merged
merged 4 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"crypto/subtle"
"encoding/base32"
"encoding/base64"
"net/http"
"testing"
Expand Down Expand Up @@ -147,5 +148,5 @@ func TestTokenFromRequest(t *testing.T) {

// randomAPIKeyString is a helper.
func randomAPIKeyString() string {
return base64.URLEncoding.EncodeToString(fastrand.Bytes(database.PubKeySize))
return base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(fastrand.Bytes(database.PubKeySize))
}
30 changes: 23 additions & 7 deletions database/apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package database

import (
"context"
"encoding/base64"
"encoding/base32"
"strings"
"time"

"gitlab.com/NebulousLabs/errors"
Expand Down Expand Up @@ -31,7 +32,8 @@ var (
)

type (
// APIKey is a base64URL-encoded representation of []byte with length PubKeySize
// APIKey is a base32hex-encoded-no-padding representation of []byte with
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
// length PubKeySize
APIKey string
// APIKeyRecord is a non-expiring authentication token generated on user demand.
APIKeyRecord struct {
Expand All @@ -42,13 +44,27 @@ type (
}
)

// Bytes defines the way we decode an API key.
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
func (ak APIKey) Bytes() ([]byte, error) {
return base32.HexEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(string(ak)))
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
}

// IsValid checks whether the underlying string satisfies the type's requirement
// to represent a []byte with length PubKeySize which is encoded as base64URL.
// to represent a []byte with length PubKeySize which is encoded as base32 with
// no padding.
// This method does NOT check whether the API exists in the database.
func (ak APIKey) IsValid() bool {
b := make([]byte, PubKeySize)
n, err := base64.URLEncoding.Decode(b, []byte(ak))
return err == nil && n == PubKeySize
b, err := ak.Bytes()
return err == nil && len(b) == PubKeySize
peterjan marked this conversation as resolved.
Show resolved Hide resolved
}

// LoadBytes encodes a [PubKeySize]byte into an API key.
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
func (ak *APIKey) LoadBytes(b []byte) error {
if len(b) != PubKeySize {
return errors.New("key too short")
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
}
*ak = APIKey(base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(b))
return nil
}

// APIKeyCreate creates a new API key.
Expand All @@ -65,7 +81,7 @@ func (db *DB) APIKeyCreate(ctx context.Context, user User) (*APIKeyRecord, error
}
ak := APIKeyRecord{
UserID: user.ID,
Key: APIKey(base64.URLEncoding.EncodeToString(fastrand.Bytes(PubKeySize))),
Key: APIKey(base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(fastrand.Bytes(PubKeySize))),
ro-tex marked this conversation as resolved.
Show resolved Hide resolved
CreatedAt: time.Now().UTC(),
}
ior, err := db.staticAPIKeys.InsertOne(ctx, ak)
Expand Down
8 changes: 4 additions & 4 deletions test/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ func TestUserTierCache(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if ul.TierName != database.UserLimits[database.TierFree].TierName {
t.Fatalf("Expected tier name '%s', got '%s'", database.UserLimits[database.TierFree].TierName, ul.TierName)
if ul.TierName != database.UserLimits[database.TierPremium20].TierName {
ChrisSchinnerl marked this conversation as resolved.
Show resolved Hide resolved
t.Fatalf("Expected tier name '%s', got '%s'", database.UserLimits[database.TierPremium20].TierName, ul.TierName)
}
if ul.TierID != database.TierFree {
t.Fatalf("Expected tier id '%d', got '%d'", database.TierFree, ul.TierID)
if ul.TierID != database.TierPremium20 {
t.Fatalf("Expected tier id '%d', got '%d'", database.TierPremium20, ul.TierID)
}
// Now set their SubscribedUntil in the future, so their subscription tier
// is active.
Expand Down