Merge branch 'authorizerdev:main' into main

This commit is contained in:
scaletech-milan 2023-12-01 14:11:20 +05:30 committed by GitHub
commit fbb4975c02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 34 deletions

View File

@ -8,8 +8,10 @@ type AuthenticatorConfig struct {
ScannerImage string ScannerImage string
// Secrets is the secret key // Secrets is the secret key
Secret string Secret string
// RecoveryCode is the secret key // RecoveryCode is the list of recovery codes
RecoveryCodes []string RecoveryCodes []string
// RecoveryCodeMap is the map of recovery codes
RecoveryCodeMap map[string]bool
} }
// Provider defines authenticators provider // Provider defines authenticators provider

View File

@ -4,12 +4,12 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"image/png" "image/png"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/pquerna/otp/totp" "github.com/pquerna/otp/totp"
log "github.com/sirupsen/logrus"
"github.com/authorizerdev/authorizer/server/authenticators/providers" "github.com/authorizerdev/authorizer/server/authenticators/providers"
"github.com/authorizerdev/authorizer/server/constants" "github.com/authorizerdev/authorizer/server/constants"
@ -22,30 +22,26 @@ import (
// Generate generates a Time-Based One-Time Password (TOTP) for a user and returns the base64-encoded QR code for frontend display. // 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) { func (p *provider) Generate(ctx context.Context, id string) (*providers.AuthenticatorConfig, error) {
var buf bytes.Buffer var buf bytes.Buffer
//get user details //get user details
user, err := db.Provider.GetUserByID(ctx, id) user, err := db.Provider.GetUserByID(ctx, id)
if err != nil { if err != nil {
return nil, fmt.Errorf("error while getting user details") return nil, err
} }
// generate totp, Authenticators hash is valid for 30 seconds // generate totp, Authenticators hash is valid for 30 seconds
key, err := totp.Generate(totp.GenerateOpts{ key, err := totp.Generate(totp.GenerateOpts{
Issuer: "authorizer", Issuer: "authorizer",
AccountName: refs.StringValue(user.Email), AccountName: refs.StringValue(user.Email),
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("error while genrating totp") return nil, err
} }
//generating image for key and encoding to base64 for displaying in frontend //generating image for key and encoding to base64 for displaying in frontend
img, err := key.Image(200, 200) img, err := key.Image(200, 200)
if err != nil { if err != nil {
return nil, fmt.Errorf("error while creating qr image for totp") return nil, err
} }
png.Encode(&buf, img) png.Encode(&buf, img)
encodedText := crypto.EncryptB64(buf.String()) encodedText := crypto.EncryptB64(buf.String())
secret := key.Secret() secret := key.Secret()
recoveryCodes := []string{} recoveryCodes := []string{}
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
@ -59,24 +55,40 @@ func (p *provider) Generate(ctx context.Context, id string) (*providers.Authenti
// Converting recoveryCodesMap to string // Converting recoveryCodesMap to string
jsonData, err := json.Marshal(recoverCodesMap) jsonData, err := json.Marshal(recoverCodesMap)
if err != nil { if err != nil {
return nil, fmt.Errorf("error while converting recoveryCodes to string") return nil, err
} }
recoveryCodesString := string(jsonData) recoveryCodesString := string(jsonData)
totpModel := &models.Authenticator{ totpModel := &models.Authenticator{
Secret: secret, Secret: secret,
RecoveryCodes: refs.NewStringRef(recoveryCodesString), RecoveryCodes: refs.NewStringRef(recoveryCodesString),
UserID: user.ID, UserID: user.ID,
Method: constants.EnvKeyTOTPAuthenticator, Method: constants.EnvKeyTOTPAuthenticator,
} }
_, err = db.Provider.AddAuthenticator(ctx, totpModel) authenticator, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, user.ID, constants.EnvKeyTOTPAuthenticator)
if err != nil { if err != nil {
return nil, fmt.Errorf("error while inserting into totp table") 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
}
} }
return &providers.AuthenticatorConfig{ return &providers.AuthenticatorConfig{
ScannerImage: encodedText, ScannerImage: encodedText,
Secret: secret, Secret: secret,
RecoveryCodes: recoveryCodes, RecoveryCodes: recoveryCodes,
RecoveryCodeMap: recoverCodesMap,
}, nil }, nil
} }
@ -85,22 +97,18 @@ func (p *provider) Validate(ctx context.Context, passcode string, userID string)
// get totp details // get totp details
totpModel, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, userID, constants.EnvKeyTOTPAuthenticator) totpModel, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, userID, constants.EnvKeyTOTPAuthenticator)
if err != nil { if err != nil {
return false, fmt.Errorf("error while getting totp details from authenticators") return false, err
} }
// validate totp
status := totp.Validate(passcode, totpModel.Secret) 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 // checks if user not signed in for totp and totp code is correct then VerifiedAt will be stored in db
if totpModel.VerifiedAt == nil { if totpModel.VerifiedAt == nil && status {
if status { timeNow := time.Now().Unix()
timeNow := time.Now().Unix() totpModel.VerifiedAt = &timeNow
totpModel.VerifiedAt = &timeNow _, err = db.Provider.UpdateAuthenticator(ctx, totpModel)
_, err = db.Provider.UpdateAuthenticator(ctx, totpModel) if err != nil {
if err != nil { return false, err
return false, fmt.Errorf("error while updaing authenticator table for totp")
}
return status, nil
} }
return status, nil
} }
return status, nil return status, nil
} }

View File

@ -35,13 +35,10 @@ func (p *provider) AddAuthenticator(ctx context.Context, authenticators *models.
func (p *provider) UpdateAuthenticator(ctx context.Context, authenticators *models.Authenticator) (*models.Authenticator, error) { func (p *provider) UpdateAuthenticator(ctx context.Context, authenticators *models.Authenticator) (*models.Authenticator, error) {
authenticators.UpdatedAt = time.Now().Unix() authenticators.UpdatedAt = time.Now().Unix()
result := p.db.Save(&authenticators) result := p.db.Save(&authenticators)
if result.Error != nil { if result.Error != nil {
return authenticators, result.Error return authenticators, result.Error
} }
return authenticators, nil return authenticators, nil
} }

View File

@ -244,8 +244,8 @@ func LoginResolver(ctx context.Context, params model.LoginInput) (*model.AuthRes
return nil, err return nil, err
} }
authenticator, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, user.ID, constants.EnvKeyTOTPAuthenticator) authenticator, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, user.ID, constants.EnvKeyTOTPAuthenticator)
// Check if it's the first time user or if their TOTP is not verified if err != nil || authenticator == nil || authenticator.VerifiedAt == nil {
if err != nil || ((authenticator == nil) || (authenticator != nil && authenticator.VerifiedAt == nil)) { // generate totp
// Generate a base64 URL and initiate the registration for TOTP // Generate a base64 URL and initiate the registration for TOTP
authConfig, err := authenticators.Provider.Generate(ctx, user.ID) authConfig, err := authenticators.Provider.Generate(ctx, user.ID)
if err != nil { if err != nil {

View File

@ -58,10 +58,14 @@ func VerifyOtpResolver(ctx context.Context, params model.VerifyOTPRequest) (*mod
// Verify OTP based on TOPT or OTP // Verify OTP based on TOPT or OTP
if refs.BoolValue(params.Totp) { if refs.BoolValue(params.Totp) {
status, err := authenticators.Provider.Validate(ctx, params.Otp, user.ID) status, err := authenticators.Provider.Validate(ctx, params.Otp, user.ID)
if err != nil || !status { if err != nil {
log.Debug("Failed to validate totp: ", err) log.Debug("Failed to validate totp: ", err)
return nil, fmt.Errorf("error while validating passcode") return nil, fmt.Errorf("error while validating passcode")
} }
if !status {
log.Debug("Failed to verify otp request: Incorrect value")
return res, fmt.Errorf(`invalid otp`)
}
} else { } else {
var otp *models.OTP var otp *models.OTP
if currentField == models.FieldNameEmail { if currentField == models.FieldNameEmail {