2023-11-16 13:00:54 +00:00
|
|
|
package totp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-12-03 03:33:22 +00:00
|
|
|
"fmt"
|
2023-11-16 13:00:54 +00:00
|
|
|
"image/png"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/pquerna/otp/totp"
|
2023-12-01 08:30:01 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-11-16 13:00:54 +00:00
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/authenticators/providers"
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/authorizerdev/authorizer/server/crypto"
|
|
|
|
"github.com/authorizerdev/authorizer/server/db"
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Generate generates a Time-Based One-Time Password (TOTP) for a user and returns the base64-encoded QR code for frontend display.
|
|
|
|
func (p *provider) Generate(ctx context.Context, id string) (*providers.AuthenticatorConfig, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
//get user details
|
|
|
|
user, err := db.Provider.GetUserByID(ctx, id)
|
|
|
|
if err != nil {
|
2023-12-01 08:30:01 +00:00
|
|
|
return nil, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
// generate totp, Authenticators hash is valid for 30 seconds
|
|
|
|
key, err := totp.Generate(totp.GenerateOpts{
|
|
|
|
Issuer: "authorizer",
|
|
|
|
AccountName: refs.StringValue(user.Email),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-12-01 08:30:01 +00:00
|
|
|
return nil, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
//generating image for key and encoding to base64 for displaying in frontend
|
|
|
|
img, err := key.Image(200, 200)
|
|
|
|
if err != nil {
|
2023-12-01 08:30:01 +00:00
|
|
|
return nil, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
png.Encode(&buf, img)
|
|
|
|
encodedText := crypto.EncryptB64(buf.String())
|
|
|
|
secret := key.Secret()
|
|
|
|
recoveryCodes := []string{}
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
recoveryCodes = append(recoveryCodes, uuid.NewString())
|
|
|
|
}
|
|
|
|
// Converting recoveryCodes to string
|
|
|
|
recoverCodesMap := map[string]bool{}
|
|
|
|
for i := 0; i < len(recoveryCodes); i++ {
|
|
|
|
recoverCodesMap[recoveryCodes[i]] = false
|
|
|
|
}
|
|
|
|
// Converting recoveryCodesMap to string
|
|
|
|
jsonData, err := json.Marshal(recoverCodesMap)
|
|
|
|
if err != nil {
|
2023-12-01 08:30:01 +00:00
|
|
|
return nil, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
recoveryCodesString := string(jsonData)
|
|
|
|
totpModel := &models.Authenticator{
|
|
|
|
Secret: secret,
|
|
|
|
RecoveryCodes: refs.NewStringRef(recoveryCodesString),
|
|
|
|
UserID: user.ID,
|
|
|
|
Method: constants.EnvKeyTOTPAuthenticator,
|
|
|
|
}
|
2023-12-01 08:30:01 +00:00
|
|
|
authenticator, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, user.ID, constants.EnvKeyTOTPAuthenticator)
|
2023-11-16 13:00:54 +00:00
|
|
|
if err != nil {
|
2023-12-01 08:30:01 +00:00
|
|
|
log.Debug("Failed to get authenticator details by user id, creating new record: ", err)
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
if authenticator == nil {
|
|
|
|
// if authenticator is nil then create new authenticator
|
|
|
|
_, err = db.Provider.AddAuthenticator(ctx, totpModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
authenticator.Secret = secret
|
|
|
|
authenticator.RecoveryCodes = refs.NewStringRef(recoveryCodesString)
|
|
|
|
// if authenticator is not nil then update authenticator
|
|
|
|
_, err = db.Provider.UpdateAuthenticator(ctx, authenticator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
return &providers.AuthenticatorConfig{
|
2023-12-01 08:30:01 +00:00
|
|
|
ScannerImage: encodedText,
|
|
|
|
Secret: secret,
|
|
|
|
RecoveryCodes: recoveryCodes,
|
|
|
|
RecoveryCodeMap: recoverCodesMap,
|
2023-11-16 13:00:54 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates a Time-Based One-Time Password (TOTP) against the stored TOTP secret for a user.
|
|
|
|
func (p *provider) Validate(ctx context.Context, passcode string, userID string) (bool, error) {
|
|
|
|
// get totp details
|
|
|
|
totpModel, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, userID, constants.EnvKeyTOTPAuthenticator)
|
|
|
|
if err != nil {
|
2023-12-01 08:30:01 +00:00
|
|
|
return false, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
2023-12-01 08:30:01 +00:00
|
|
|
// validate totp
|
2023-11-16 13:00:54 +00:00
|
|
|
status := totp.Validate(passcode, totpModel.Secret)
|
|
|
|
// checks if user not signed in for totp and totp code is correct then VerifiedAt will be stored in db
|
2023-12-01 08:30:01 +00:00
|
|
|
if totpModel.VerifiedAt == nil && status {
|
|
|
|
timeNow := time.Now().Unix()
|
|
|
|
totpModel.VerifiedAt = &timeNow
|
|
|
|
_, err = db.Provider.UpdateAuthenticator(ctx, totpModel)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
2023-12-03 03:33:22 +00:00
|
|
|
// ValidateRecoveryCode validates a Time-Based One-Time Password (TOTP) recovery code against the stored TOTP recovery code for a user.
|
|
|
|
func (p *provider) ValidateRecoveryCode(ctx context.Context, recoveryCode, userID string) (bool, error) {
|
2023-11-16 13:00:54 +00:00
|
|
|
// get totp details
|
2023-12-03 03:33:22 +00:00
|
|
|
totpModel, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, userID, constants.EnvKeyTOTPAuthenticator)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// convert recoveryCodes to map
|
|
|
|
recoveryCodesMap := map[string]bool{}
|
|
|
|
err = json.Unmarshal([]byte(refs.StringValue(totpModel.RecoveryCodes)), &recoveryCodesMap)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// check if recovery code is valid
|
|
|
|
if val, ok := recoveryCodesMap[recoveryCode]; !ok {
|
|
|
|
return false, fmt.Errorf("invalid recovery code")
|
|
|
|
} else if val {
|
|
|
|
return false, fmt.Errorf("recovery code already used")
|
|
|
|
}
|
|
|
|
// update recovery code map
|
|
|
|
recoveryCodesMap[recoveryCode] = true
|
|
|
|
// convert recoveryCodesMap to string
|
|
|
|
jsonData, err := json.Marshal(recoveryCodesMap)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
recoveryCodesString := string(jsonData)
|
|
|
|
totpModel.RecoveryCodes = refs.NewStringRef(recoveryCodesString)
|
|
|
|
// update recovery code map in db
|
|
|
|
_, err = db.Provider.UpdateAuthenticator(ctx, totpModel)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
2023-11-16 13:00:54 +00:00
|
|
|
}
|