| 1 |
// Copyright 2024 OpenPubkey |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
// |
| 15 |
// SPDX-License-Identifier: Apache-2.0 |
| 16 |
|
| 17 |
package mocks |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/rand" |
| 21 |
"encoding/hex" |
| 22 |
"encoding/json" |
| 23 |
"fmt" |
| 24 |
"strings" |
| 25 |
"sync" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/openpubkey/openpubkey/cosigner" |
| 29 |
"github.com/openpubkey/openpubkey/pktoken" |
| 30 |
) |
| 31 |
|
| 32 |
// This is intended for testing purposes. The locking strategy used is not |
| 33 |
// particularly efficient. Anyone building a Cosigner should use the interface |
| 34 |
// above to replace this in-memory store with a database. |
| 35 |
type AuthStateInMemoryStore struct { |
| 36 |
AuthIDIssuer *cosigner.AuthIDIssuer |
| 37 |
AuthStateMap map[string]*cosigner.AuthState |
| 38 |
AuthCodeMap map[string]string |
| 39 |
AuthStateMapLock sync.RWMutex |
| 40 |
AuthcodeMapLock sync.RWMutex |
| 41 |
} |
| 42 |
|
| 43 |
func NewAuthStateInMemoryStore(hmacKey []byte) *AuthStateInMemoryStore { |
| 44 |
return &AuthStateInMemoryStore{ |
| 45 |
AuthStateMap: make(map[string]*cosigner.AuthState), |
| 46 |
AuthCodeMap: make(map[string]string), |
| 47 |
AuthcodeMapLock: sync.RWMutex{}, |
| 48 |
AuthStateMapLock: sync.RWMutex{}, |
| 49 |
AuthIDIssuer: cosigner.NewAuthIDIssuer(hmacKey), |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Writes to the AuthState are not concurrency safe, do not write |
| 54 |
func (s *AuthStateInMemoryStore) LookupAuthState(authID string) (*cosigner.AuthState, bool) { |
| 55 |
s.AuthStateMapLock.RLock() |
| 56 |
as, ok := s.AuthStateMap[authID] |
| 57 |
s.AuthStateMapLock.RUnlock() |
| 58 |
return as, ok // Pass by value to prevent writes to the original |
| 59 |
} |
| 60 |
|
| 61 |
func (s *AuthStateInMemoryStore) UpdateAuthState(authID string, authState cosigner.AuthState) error { |
| 62 |
s.AuthStateMapLock.Lock() |
| 63 |
defer s.AuthStateMapLock.Unlock() |
| 64 |
|
| 65 |
if _, ok := s.AuthStateMap[authID]; !ok { |
| 66 |
return fmt.Errorf("failed to upload auth session because authID specified matches no session") |
| 67 |
} else { |
| 68 |
s.AuthStateMap[authID] = &authState |
| 69 |
return nil |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func (s *AuthStateInMemoryStore) CreateNewAuthSession(pkt *pktoken.PKToken, ruri string, nonce string) (string, error) { |
| 74 |
var claims struct { |
| 75 |
Issuer string `json:"iss"` |
| 76 |
Aud any `json:"aud"` |
| 77 |
Sub string `json:"sub"` |
| 78 |
Email string `json:"email"` |
| 79 |
} |
| 80 |
if err := json.Unmarshal(pkt.Payload, &claims); err != nil { |
| 81 |
return "", fmt.Errorf("failed to unmarshal PK Token: %w", err) |
| 82 |
} |
| 83 |
// An audience can be a string or an array of strings. |
| 84 |
// |
| 85 |
// RFC-7519 JSON Web Token (JWT) says: |
| 86 |
// "In the general case, the "aud" value is an array of case- |
| 87 |
// sensitive strings, each containing a StringOrURI value. In the |
| 88 |
// special case when the JWT has one audience, the "aud" value MAY be a |
| 89 |
// single case-sensitive string containing a StringOrURI value." |
| 90 |
// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3 |
| 91 |
var audience string |
| 92 |
switch t := claims.Aud.(type) { |
| 93 |
case string: |
| 94 |
audience = t |
| 95 |
case []any: |
| 96 |
audList := []string{} |
| 97 |
for _, v := range t { |
| 98 |
audList = append(audList, v.(string)) |
| 99 |
} |
| 100 |
audience = strings.Join(audList, ",") |
| 101 |
default: |
| 102 |
return "", fmt.Errorf("failed to deserialize aud (audience) claim in ID Token: %T", t) |
| 103 |
} |
| 104 |
|
| 105 |
authState := &cosigner.AuthState{ |
| 106 |
Pkt: pkt, |
| 107 |
Issuer: claims.Issuer, |
| 108 |
Aud: audience, |
| 109 |
Sub: claims.Sub, |
| 110 |
Username: claims.Email, |
| 111 |
DisplayName: strings.Split(claims.Email, "@")[0], //TODO: Use full name from ID Token |
| 112 |
RedirectURI: ruri, |
| 113 |
Nonce: nonce, |
| 114 |
AuthcodeRedeemed: false, |
| 115 |
AuthcodeIssued: false, |
| 116 |
} |
| 117 |
|
| 118 |
if authID, err := s.AuthIDIssuer.CreateAuthID(uint64(time.Now().Unix())); err != nil { |
| 119 |
return "", err |
| 120 |
} else { |
| 121 |
s.AuthStateMapLock.Lock() |
| 122 |
if _, ok := s.AuthStateMap[authID]; ok { |
| 123 |
return "", fmt.Errorf("specified authID is already in use") |
| 124 |
} |
| 125 |
s.AuthStateMap[authID] = authState |
| 126 |
s.AuthStateMapLock.Unlock() |
| 127 |
return authID, nil |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
func (s *AuthStateInMemoryStore) CreateAuthcode(authID string) (string, error) { |
| 132 |
authCodeBytes := make([]byte, 32) |
| 133 |
if _, err := rand.Read(authCodeBytes); err != nil { |
| 134 |
return "", err |
| 135 |
} |
| 136 |
authcode := hex.EncodeToString(authCodeBytes) |
| 137 |
|
| 138 |
// We take a full read write lock here to ensure we don't issue an authcode twice for the same session |
| 139 |
s.AuthStateMapLock.Lock() |
| 140 |
defer s.AuthStateMapLock.Unlock() |
| 141 |
|
| 142 |
if authState, ok := s.AuthStateMap[authID]; !ok { |
| 143 |
return "", fmt.Errorf("no such authID") |
| 144 |
} else if authState.AuthcodeIssued { |
| 145 |
return "", fmt.Errorf("authcode already issued for this authID") |
| 146 |
} else { |
| 147 |
s.AuthcodeMapLock.Lock() |
| 148 |
defer s.AuthcodeMapLock.Unlock() |
| 149 |
|
| 150 |
if _, ok := s.AuthCodeMap[authcode]; ok { |
| 151 |
return "", fmt.Errorf("authcode collision implies randomness failure in RNG") |
| 152 |
} |
| 153 |
authState.AuthcodeIssued = true |
| 154 |
s.AuthCodeMap[authcode] = authID |
| 155 |
|
| 156 |
return authcode, nil |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func (s *AuthStateInMemoryStore) RedeemAuthcode(authcode string) (cosigner.AuthState, string, error) { |
| 161 |
s.AuthcodeMapLock.RLock() |
| 162 |
authID, authcodeFound := s.AuthCodeMap[authcode] |
| 163 |
s.AuthcodeMapLock.RUnlock() |
| 164 |
if !authcodeFound { |
| 165 |
return cosigner.AuthState{}, "", fmt.Errorf("invalid authcode") |
| 166 |
} else { |
| 167 |
s.AuthStateMapLock.Lock() |
| 168 |
defer s.AuthStateMapLock.Unlock() |
| 169 |
|
| 170 |
authState := s.AuthStateMap[authID] |
| 171 |
if !authState.AuthcodeIssued { |
| 172 |
// This should never happen |
| 173 |
return cosigner.AuthState{}, "", fmt.Errorf("no authcode issued for this authID") |
| 174 |
} |
| 175 |
if authState.AuthcodeRedeemed { |
| 176 |
return cosigner.AuthState{}, "", fmt.Errorf("authcode has already been redeemed") |
| 177 |
} |
| 178 |
authState.AuthcodeRedeemed = true |
| 179 |
return *authState, authID, nil |
| 180 |
} |
| 181 |
} |