Skip to content

Commit

Permalink
add offline key for bootstrapping problem
Browse files Browse the repository at this point in the history
  • Loading branch information
1lann committed Oct 1, 2023
1 parent 27e713e commit 4b58a3a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions tmpauth/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type accessController struct {
realm string
clientID string
secret []byte
offlineKey string
publicKey *ecdsa.PublicKey
tokenCache map[[32]byte]*CachedToken
tokenCacheMutex *sync.RWMutex
Expand Down
6 changes: 6 additions & 0 deletions tmpauth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tmpauth

import (
"crypto/sha256"
"crypto/subtle"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -14,12 +15,17 @@ import (
)

const TmpAuthHost = "auth.tmpim.pw"
const offlineUser = "offline"

func (ac *accessController) authenticateUser(username, password string) error {
ac.janitorOnce.Do(func() {
go ac.janitor()
})

if username == offlineUser && subtle.ConstantTimeCompare([]byte(password), []byte(ac.offlineKey)) == 1 {
return nil
}

token, err := ac.parseWrappedAuthJWT(password)
if err != nil {
return err
Expand Down
14 changes: 10 additions & 4 deletions tmpauth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func newAccessController(options map[string]interface{}) (auth.AccessController,
return nil, fmt.Errorf(`"publickey" must be set for tmpauth access controller`)
}

offlineKey, ok := options["offlinekey"].(string)
if !ok {
return nil, fmt.Errorf(`"offlinekey" must be set for tmpauth access controller`)
}

pubKeyData, err := base64.StdEncoding.DecodeString(publicKey)
if err != nil {
return nil, fmt.Errorf("tmpauth: invalid public_key: %w", err)
Expand Down Expand Up @@ -69,10 +74,11 @@ func newAccessController(options map[string]interface{}) (auth.AccessController,
}

return &accessController{
realm: realm,
clientID: claims.Subject,
secret: []byte(claims.Secret),
publicKey: pubKey,
realm: realm,
clientID: claims.Subject,
secret: []byte(claims.Secret),
offlineKey: offlineKey,
publicKey: pubKey,

tokenCache: make(map[[32]byte]*CachedToken),
tokenCacheMutex: new(sync.RWMutex),
Expand Down

0 comments on commit 4b58a3a

Please sign in to comment.