-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Two factor authentication support #630
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d810cc4
Initial commit for 2FA support
minecrafter 4a9eef0
Add vendored files
minecrafter 462ee3c
Add missing depends
minecrafter 723f457
A few clean ups
minecrafter ac94be1
Added improvements, proper encryption
minecrafter c9feca7
Better encryption key
minecrafter 901e00a
Simplify "key" generation
minecrafter 2130fb9
Make 2FA enrollment page more robust
minecrafter 004d503
Fix typo
minecrafter 27d4ae1
Rename twofa/2FA to TwoFactor
minecrafter 8f9d554
UNIQUE INDEX -> UNIQUE
minecrafter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,6 +203,10 @@ func runWeb(ctx *cli.Context) error { | |
m.Post("/sign_up", bindIgnErr(auth.RegisterForm{}), user.SignUpPost) | ||
m.Get("/reset_password", user.ResetPasswd) | ||
m.Post("/reset_password", user.ResetPasswdPost) | ||
m.Get("/2fa", user.ShowTwofa) | ||
m.Post("/2fa", bindIgnErr(auth.TwofaAuthForm{}), user.TwofaPost) | ||
m.Get("/2fa_scratch", user.TwofaScratch) | ||
m.Post("/2fa_scratch", bindIgnErr(auth.TwofaScratchAuthForm{}), user.TwofaScratchPost) | ||
}, reqSignOut) | ||
|
||
m.Group("/user/settings", func() { | ||
|
@@ -223,6 +227,11 @@ func runWeb(ctx *cli.Context) error { | |
Post(bindIgnErr(auth.NewAccessTokenForm{}), user.SettingsApplicationsPost) | ||
m.Post("/applications/delete", user.SettingsDeleteApplication) | ||
m.Route("/delete", "GET,POST", user.SettingsDelete) | ||
m.Get("/2fa", user.SettingsTwofa) | ||
m.Post("/2fa/regenerate_scratch", user.SettingsTwofaRegenerateScratch) | ||
m.Post("/2fa/disable", user.SettingsTwofaDisable) | ||
m.Get("/2fa/enroll", user.SettingsTwofaEnroll) | ||
m.Post("/2fa/enroll", bindIgnErr(auth.TwofaAuthForm{}), user.SettingsTwofaEnrollPost) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
}, reqSignIn, func(ctx *context.Context) { | ||
ctx.Data["PageIsUserSettings"] = true | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// Copyright 2017 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package models | ||
|
||
import ( | ||
"crypto/subtle" | ||
"encoding/base64" | ||
"time" | ||
|
||
"github.com/Unknwon/com" | ||
"github.com/go-xorm/xorm" | ||
"github.com/pquerna/otp/totp" | ||
|
||
"code.gitea.io/gitea/modules/base" | ||
) | ||
|
||
// Twofa represents a two-factor authentication token. | ||
type Twofa struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
UID int64 `xorm:"UNIQUE INDEX"` | ||
Secret string | ||
ScratchToken string | ||
|
||
Created time.Time `xorm:"-"` | ||
CreatedUnix int64 `xorm:"INDEX"` | ||
Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet. | ||
UpdatedUnix int64 `xorm:"INDEX"` | ||
} | ||
|
||
// BeforeInsert will be invoked by XORM before inserting a record representing this object. | ||
func (t *Twofa) BeforeInsert() { | ||
t.CreatedUnix = time.Now().Unix() | ||
} | ||
|
||
// BeforeUpdate is invoked from XORM before updating this object. | ||
func (t *Twofa) BeforeUpdate() { | ||
t.UpdatedUnix = time.Now().Unix() | ||
} | ||
|
||
// AfterSet is invoked from XORM after setting the value of a field of this object. | ||
func (t *Twofa) AfterSet(colName string, _ xorm.Cell) { | ||
switch colName { | ||
case "created_unix": | ||
t.Created = time.Unix(t.CreatedUnix, 0).Local() | ||
case "updated_unix": | ||
t.Updated = time.Unix(t.UpdatedUnix, 0).Local() | ||
} | ||
} | ||
|
||
// GenerateScratchToken recreates the scratch token the user is using. | ||
func (t *Twofa) GenerateScratchToken() error { | ||
token, err := base.GetRandomString(8) | ||
if err != nil { | ||
return err | ||
} | ||
t.ScratchToken = token | ||
return nil | ||
} | ||
|
||
// VerifyScratchToken verifies if the specified scratch token is valid. | ||
func (t *Twofa) VerifyScratchToken(token string) bool { | ||
if len(token) == 0 { | ||
return false | ||
} | ||
return subtle.ConstantTimeCompare([]byte(token), []byte(t.ScratchToken)) == 1 | ||
} | ||
|
||
// TODO: Actually implement. For now, this is a static key. | ||
func getKey() ([]byte, error) { | ||
k := make([]byte, 16) | ||
for i := 0; i < 16; i++ { | ||
k[i] = byte(i + 1) | ||
} | ||
return k, nil | ||
} | ||
|
||
// SetSecret sets the 2FA secret. | ||
func (t *Twofa) SetSecret(secret string) error { | ||
k, err := getKey() | ||
if err != nil { | ||
return err | ||
} | ||
secretBytes, err := com.AESEncrypt(k, []byte(secret)) | ||
if err != nil { | ||
return err | ||
} | ||
t.Secret = base64.StdEncoding.EncodeToString(secretBytes) | ||
return nil | ||
} | ||
|
||
// ValidateTOTP validates the provided passcode. | ||
func (t *Twofa) ValidateTOTP(passcode string) (bool, error) { | ||
k, err := getKey() | ||
if err != nil { | ||
return false, err | ||
} | ||
decodedStoredSecret, err := base64.StdEncoding.DecodeString(t.Secret) | ||
if err != nil { | ||
return false, err | ||
} | ||
secret, err := com.AESDecrypt(k, decodedStoredSecret) | ||
if err != nil { | ||
return false, err | ||
} | ||
secretStr := string(secret) | ||
return totp.Validate(passcode, secretStr), nil | ||
} | ||
|
||
// NewTwofa creates a new two-factor authentication token. | ||
func NewTwofa(t *Twofa) error { | ||
err := t.GenerateScratchToken() | ||
if err != nil { | ||
return err | ||
} | ||
_, err = x.Insert(t) | ||
return err | ||
} | ||
|
||
// UpdateTwofa updates a two-factor authentication token. | ||
func UpdateTwofa(t *Twofa) error { | ||
_, err := x.Id(t.ID).AllCols().Update(t) | ||
return err | ||
} | ||
|
||
// GetTwofaByUID returns the two-factor authentication token associated with | ||
// the user, if any. | ||
func GetTwofaByUID(uid int64) (*Twofa, error) { | ||
twofa := &Twofa{UID: uid} | ||
has, err := x.Get(twofa) | ||
if err != nil { | ||
return nil, err | ||
} else if !has { | ||
return nil, ErrTwofaNotEnrolled{uid} | ||
} | ||
return twofa, nil | ||
} | ||
|
||
// DeleteTwofaByID deletes two-factor authentication token by given ID. | ||
func DeleteTwofaByID(id, userID int64) error { | ||
cnt, err := x.Id(id).Delete(&Twofa{ | ||
UID: userID, | ||
}) | ||
if err != nil { | ||
return err | ||
} else if cnt != 1 { | ||
return ErrTwofaNotEnrolled{userID} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there are many endpoints it might be better to use the following structure and change 2fa_scratch to be 2fa/scratch.