2021-12-25 02:16:58 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO - Learn how to use https://github.com/golang/oauth2 instead
|
|
|
|
// TODO - Look into jwt, etc.
|
2022-06-07 19:25:14 +02:00
|
|
|
// For now I just want a process that's shaped like what I'm looking for.
|
2022-06-09 23:04:49 +02:00
|
|
|
// (email/password, encrypted wallets, hmac, sequence (eventually lastSynced), etc)
|
2021-12-25 02:16:58 +01:00
|
|
|
|
2022-06-07 19:25:14 +02:00
|
|
|
type UserId int32
|
|
|
|
type Email string
|
|
|
|
type DeviceId string
|
|
|
|
type Password string
|
2022-06-09 23:04:49 +02:00
|
|
|
type TokenString string
|
2021-12-25 02:16:58 +01:00
|
|
|
type AuthScope string
|
|
|
|
|
|
|
|
const ScopeFull = AuthScope("*")
|
|
|
|
|
|
|
|
// For test stubs
|
|
|
|
type AuthInterface interface {
|
2022-06-07 19:25:14 +02:00
|
|
|
// TODO maybe have a "refresh token" thing if the client won't have email available all the time?
|
|
|
|
NewToken(UserId, DeviceId, AuthScope) (*AuthToken, error)
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Auth struct{}
|
|
|
|
|
|
|
|
// Note that everything here is given to anybody who presents a valid
|
|
|
|
// downloadKey and associated email. Currently these fields are safe to give
|
|
|
|
// at that low security level, but keep this in mind as we change this struct.
|
|
|
|
type AuthToken struct {
|
2022-06-09 23:04:49 +02:00
|
|
|
Token TokenString `json:"token"`
|
|
|
|
DeviceId DeviceId `json:"deviceId"`
|
|
|
|
Scope AuthScope `json:"scope"`
|
|
|
|
UserId UserId `json:"userId"`
|
|
|
|
Expiration *time.Time `json:"expiration"`
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const AuthTokenLength = 32
|
|
|
|
|
2022-06-07 19:25:14 +02:00
|
|
|
func (a *Auth) NewToken(userId UserId, deviceId DeviceId, scope AuthScope) (*AuthToken, error) {
|
2021-12-25 02:16:58 +01:00
|
|
|
b := make([]byte, AuthTokenLength)
|
2022-06-07 19:25:14 +02:00
|
|
|
// TODO - Is this is a secure random function? (Maybe audit)
|
2021-12-25 02:16:58 +01:00
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error generating token: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &AuthToken{
|
2022-06-09 23:04:49 +02:00
|
|
|
Token: TokenString(hex.EncodeToString(b)),
|
2022-06-07 19:25:14 +02:00
|
|
|
DeviceId: deviceId,
|
|
|
|
Scope: scope,
|
|
|
|
UserId: userId,
|
2021-12-25 02:16:58 +01:00
|
|
|
// TODO add Expiration here instead of putting it in store.go. and thus redo store.go. d'oh.
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE - not stubbing methods of structs like this. more convoluted than it's worth right now
|
|
|
|
func (at *AuthToken) ScopeValid(required AuthScope) bool {
|
2022-06-08 00:41:32 +02:00
|
|
|
// So far * is the only scope issued. Used to have more, didn't want to
|
|
|
|
// delete this feature yet in case we add more again. We'll delete it if it's
|
|
|
|
// of no use and ends up complicating anything.
|
|
|
|
return at.Scope == ScopeFull || at.Scope == required
|
2021-12-25 02:16:58 +01:00
|
|
|
}
|
2022-06-07 19:25:14 +02:00
|
|
|
|
|
|
|
func (p Password) Obfuscate() string {
|
|
|
|
// TODO KDF instead
|
|
|
|
hash := sha256.Sum256([]byte(p))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
}
|