Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minor mfa changes #692

Merged
merged 3 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions api/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"time"

"fmt"
"net/url"

"github.com/aaronarduino/goqrsvg"
svg "github.com/ajstarks/svgo"
"github.com/boombuler/barcode/qr"
Expand All @@ -16,7 +18,6 @@ import (
"github.com/netlify/gotrue/storage"
"github.com/netlify/gotrue/utilities"
"github.com/pquerna/otp/totp"
"net/url"
)

type EnrollFactorParams struct {
Expand Down Expand Up @@ -87,34 +88,33 @@ func (a *API) EnrollFactor(w http.ResponseWriter, r *http.Request) error {
// Read from DB for certainty
factors, err := models.FindVerifiedFactorsByUser(a.db, user)
if err != nil {
return internalServerError("Error validating number of factors in system")
return internalServerError("error validating number of factors in system").WithInternalError(err)
}
// Remove this at v2
if len(factors) >= 1 {
return forbiddenError("Only one factor can be enrolled at a time, please unenroll to continue")
return forbiddenError("only one factor can be enrolled at a time, please unenroll to continue")
}

key, err := totp.Generate(totp.GenerateOpts{
Issuer: issuer,
AccountName: user.GetEmail(),
})
if err != nil {
return internalServerError("Error generating QR Code secret key").WithInternalError(err)
return internalServerError("error generating QR Code secret key").WithInternalError(err)
}
var buf bytes.Buffer
s := svg.New(&buf)
qrCode, _ := qr.Encode(key.String(), qr.M, qr.Auto)
qs := goqrsvg.NewQrSVG(qrCode, DefaultQRSize)
qs.StartQrSVG(s)
err = qs.WriteQrSVG(s)
if err != nil {
return internalServerError("Error writing to QR Code").WithInternalError(err)
if err = qs.WriteQrSVG(s); err != nil {
return internalServerError("error writing to QR Code").WithInternalError(err)
}
s.End()

factor, terr := models.NewFactor(user, params.FriendlyName, params.FactorType, models.FactorUnverifiedState, key.Secret())
if terr != nil {
return internalServerError("Database error creating factor").WithInternalError(err)
return internalServerError("database error creating factor").WithInternalError(err)
}
terr = a.db.Transaction(func(tx *storage.Connection) error {
if terr = tx.Create(factor); terr != nil {
Expand Down
9 changes: 5 additions & 4 deletions models/factor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package models

import (
"database/sql"
"time"

"github.com/gofrs/uuid"
"github.com/netlify/gotrue/storage"
"github.com/pkg/errors"
"time"
)

const FactorUnverifiedState = "unverified"
Expand All @@ -16,12 +17,12 @@ const Webauthn = "webauthn"

type Factor struct {
ID uuid.UUID `json:"id" db:"id"`
User User `belongs_to:"user"`
UserID uuid.UUID `json:"user_id" db:"user_id"`
User User `json:"-" belongs_to:"user"`
UserID uuid.UUID `json:"-" db:"user_id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Status string `json:"status" db:"status"`
FriendlyName string `json:"friendly_name" db:"friendly_name"`
FriendlyName string `json:"friendly_name,omitempty" db:"friendly_name"`
TOTPSecret string `json:"-" db:"totp_secret"`
FactorType string `json:"factor_type" db:"factor_type"`
}
Expand Down