Skip to content

Commit fbb4975

Browse files
Merge branch 'authorizerdev:main' into main
2 parents e71da3d + 7f6ddca commit fbb4975

File tree

5 files changed

+45
-34
lines changed

5 files changed

+45
-34
lines changed

server/authenticators/providers/providers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ type AuthenticatorConfig struct {
88
ScannerImage string
99
// Secrets is the secret key
1010
Secret string
11-
// RecoveryCode is the secret key
11+
// RecoveryCode is the list of recovery codes
1212
RecoveryCodes []string
13+
// RecoveryCodeMap is the map of recovery codes
14+
RecoveryCodeMap map[string]bool
1315
}
1416

1517
// Provider defines authenticators provider

server/authenticators/providers/totp/totp.go

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"fmt"
87
"image/png"
98
"time"
109

1110
"github.com/google/uuid"
1211
"github.com/pquerna/otp/totp"
12+
log "github.com/sirupsen/logrus"
1313

1414
"github.com/authorizerdev/authorizer/server/authenticators/providers"
1515
"github.com/authorizerdev/authorizer/server/constants"
@@ -22,30 +22,26 @@ import (
2222
// Generate generates a Time-Based One-Time Password (TOTP) for a user and returns the base64-encoded QR code for frontend display.
2323
func (p *provider) Generate(ctx context.Context, id string) (*providers.AuthenticatorConfig, error) {
2424
var buf bytes.Buffer
25-
2625
//get user details
2726
user, err := db.Provider.GetUserByID(ctx, id)
2827
if err != nil {
29-
return nil, fmt.Errorf("error while getting user details")
28+
return nil, err
3029
}
31-
3230
// generate totp, Authenticators hash is valid for 30 seconds
3331
key, err := totp.Generate(totp.GenerateOpts{
3432
Issuer: "authorizer",
3533
AccountName: refs.StringValue(user.Email),
3634
})
3735
if err != nil {
38-
return nil, fmt.Errorf("error while genrating totp")
36+
return nil, err
3937
}
40-
4138
//generating image for key and encoding to base64 for displaying in frontend
4239
img, err := key.Image(200, 200)
4340
if err != nil {
44-
return nil, fmt.Errorf("error while creating qr image for totp")
41+
return nil, err
4542
}
4643
png.Encode(&buf, img)
4744
encodedText := crypto.EncryptB64(buf.String())
48-
4945
secret := key.Secret()
5046
recoveryCodes := []string{}
5147
for i := 0; i < 10; i++ {
@@ -59,24 +55,40 @@ func (p *provider) Generate(ctx context.Context, id string) (*providers.Authenti
5955
// Converting recoveryCodesMap to string
6056
jsonData, err := json.Marshal(recoverCodesMap)
6157
if err != nil {
62-
return nil, fmt.Errorf("error while converting recoveryCodes to string")
58+
return nil, err
6359
}
6460
recoveryCodesString := string(jsonData)
65-
6661
totpModel := &models.Authenticator{
6762
Secret: secret,
6863
RecoveryCodes: refs.NewStringRef(recoveryCodesString),
6964
UserID: user.ID,
7065
Method: constants.EnvKeyTOTPAuthenticator,
7166
}
72-
_, err = db.Provider.AddAuthenticator(ctx, totpModel)
67+
authenticator, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, user.ID, constants.EnvKeyTOTPAuthenticator)
7368
if err != nil {
74-
return nil, fmt.Errorf("error while inserting into totp table")
69+
log.Debug("Failed to get authenticator details by user id, creating new record: ", err)
70+
// continue
71+
}
72+
if authenticator == nil {
73+
// if authenticator is nil then create new authenticator
74+
_, err = db.Provider.AddAuthenticator(ctx, totpModel)
75+
if err != nil {
76+
return nil, err
77+
}
78+
} else {
79+
authenticator.Secret = secret
80+
authenticator.RecoveryCodes = refs.NewStringRef(recoveryCodesString)
81+
// if authenticator is not nil then update authenticator
82+
_, err = db.Provider.UpdateAuthenticator(ctx, authenticator)
83+
if err != nil {
84+
return nil, err
85+
}
7586
}
7687
return &providers.AuthenticatorConfig{
77-
ScannerImage: encodedText,
78-
Secret: secret,
79-
RecoveryCodes: recoveryCodes,
88+
ScannerImage: encodedText,
89+
Secret: secret,
90+
RecoveryCodes: recoveryCodes,
91+
RecoveryCodeMap: recoverCodesMap,
8092
}, nil
8193
}
8294

@@ -85,22 +97,18 @@ func (p *provider) Validate(ctx context.Context, passcode string, userID string)
8597
// get totp details
8698
totpModel, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, userID, constants.EnvKeyTOTPAuthenticator)
8799
if err != nil {
88-
return false, fmt.Errorf("error while getting totp details from authenticators")
100+
return false, err
89101
}
90-
102+
// validate totp
91103
status := totp.Validate(passcode, totpModel.Secret)
92104
// checks if user not signed in for totp and totp code is correct then VerifiedAt will be stored in db
93-
if totpModel.VerifiedAt == nil {
94-
if status {
95-
timeNow := time.Now().Unix()
96-
totpModel.VerifiedAt = &timeNow
97-
_, err = db.Provider.UpdateAuthenticator(ctx, totpModel)
98-
if err != nil {
99-
return false, fmt.Errorf("error while updaing authenticator table for totp")
100-
}
101-
return status, nil
105+
if totpModel.VerifiedAt == nil && status {
106+
timeNow := time.Now().Unix()
107+
totpModel.VerifiedAt = &timeNow
108+
_, err = db.Provider.UpdateAuthenticator(ctx, totpModel)
109+
if err != nil {
110+
return false, err
102111
}
103-
return status, nil
104112
}
105113
return status, nil
106114
}

server/db/providers/sql/authenticator.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,10 @@ func (p *provider) AddAuthenticator(ctx context.Context, authenticators *models.
3535

3636
func (p *provider) UpdateAuthenticator(ctx context.Context, authenticators *models.Authenticator) (*models.Authenticator, error) {
3737
authenticators.UpdatedAt = time.Now().Unix()
38-
3938
result := p.db.Save(&authenticators)
40-
4139
if result.Error != nil {
4240
return authenticators, result.Error
4341
}
44-
4542
return authenticators, nil
4643
}
4744

server/resolvers/login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ func LoginResolver(ctx context.Context, params model.LoginInput) (*model.AuthRes
244244
return nil, err
245245
}
246246
authenticator, err := db.Provider.GetAuthenticatorDetailsByUserId(ctx, user.ID, constants.EnvKeyTOTPAuthenticator)
247-
// Check if it's the first time user or if their TOTP is not verified
248-
if err != nil || ((authenticator == nil) || (authenticator != nil && authenticator.VerifiedAt == nil)) {
247+
if err != nil || authenticator == nil || authenticator.VerifiedAt == nil {
248+
// generate totp
249249
// Generate a base64 URL and initiate the registration for TOTP
250250
authConfig, err := authenticators.Provider.Generate(ctx, user.ID)
251251
if err != nil {

server/resolvers/verify_otp.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ func VerifyOtpResolver(ctx context.Context, params model.VerifyOTPRequest) (*mod
5858
// Verify OTP based on TOPT or OTP
5959
if refs.BoolValue(params.Totp) {
6060
status, err := authenticators.Provider.Validate(ctx, params.Otp, user.ID)
61-
if err != nil || !status {
61+
if err != nil {
6262
log.Debug("Failed to validate totp: ", err)
6363
return nil, fmt.Errorf("error while validating passcode")
6464
}
65+
if !status {
66+
log.Debug("Failed to verify otp request: Incorrect value")
67+
return res, fmt.Errorf(`invalid otp`)
68+
}
6569
} else {
6670
var otp *models.OTP
6771
if currentField == models.FieldNameEmail {

0 commit comments

Comments
 (0)