Skip to content

Commit

Permalink
Merge pull request #595 from supabase/j0_minor_mfa_fixes
Browse files Browse the repository at this point in the history
Minor MFA fixes
  • Loading branch information
J0 authored Aug 8, 2022
2 parents 9cf6fac + a4af59f commit ca67dcf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
6 changes: 4 additions & 2 deletions api/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type VerifyFactorParams struct {
type ChallengeFactorResponse struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ExpiresAt string `json:"expires_at"`
}

Expand Down Expand Up @@ -73,6 +72,9 @@ func (a *API) EnrollFactor(w http.ResponseWriter, r *http.Request) error {
if (params.FactorType != "totp") && (params.FactorType != "webauthn") {
return unprocessableEntityError("FactorType needs to be either 'totp' or 'webauthn'")
}
if params.Issuer == "" {
return unprocessableEntityError("Issuer is required")
}
// TODO(Joel): Review this portion when email is no longer a primary key
key, err := totp.Generate(totp.GenerateOpts{
Issuer: params.Issuer,
Expand Down Expand Up @@ -190,7 +192,7 @@ func (a *API) VerifyFactor(w http.ResponseWriter, r *http.Request) error {
err = a.db.Transaction(func(tx *storage.Connection) error {
if err = models.NewAuditLogEntry(tx, instanceID, user, models.VerifyFactorAction, r.RemoteAddr, map[string]interface{}{
"factor_id": factor.ID,
"challenge_id": params.ChallengeID,
"challenge_id": challenge.ID,
}); err != nil {
return err
}
Expand Down
20 changes: 17 additions & 3 deletions api/mfa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (ts *MFATestSuite) TestEnrollFactor() {
FriendlyName string
FactorType string
Issuer string
expectedCode int
ExpectedCode int
}{
{
"TOTP: Factor has friendly name",
Expand All @@ -72,6 +72,20 @@ func (ts *MFATestSuite) TestEnrollFactor() {
"supabase.com",
http.StatusOK,
},
{
"TOTP: No issuer",
"john",
"totp",
"",
http.StatusUnprocessableEntity,
},
{
"Invalid factor type",
"bob",
"",
"john.com",
http.StatusUnprocessableEntity,
},
}
for _, c := range cases {
ts.Run(c.desc, func() {
Expand All @@ -88,13 +102,13 @@ func (ts *MFATestSuite) TestEnrollFactor() {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
req.Header.Set("Content-Type", "application/json")
ts.API.handler.ServeHTTP(w, req)
require.Equal(ts.T(), http.StatusOK, w.Code)
require.Equal(ts.T(), c.ExpectedCode, w.Code)

factors, err := models.FindFactorsByUser(ts.API.db, user)
ts.Require().NoError(err)
latestFactor := factors[len(factors)-1]
require.Equal(ts.T(), models.FactorDisabledState, latestFactor.Status)
if c.FriendlyName != "" {
if c.FriendlyName != "" && c.ExpectedCode == http.StatusOK {
require.Equal(ts.T(), c.FriendlyName, latestFactor.FriendlyName)
}
})
Expand Down

0 comments on commit ca67dcf

Please sign in to comment.