.github
assets
build
cmd
contrib
custom
docker
docs
integrations
models
fixtures
migrations
access.go
access_test.go
action.go
action_list.go
action_test.go
admin.go
admin_test.go
attachment.go
attachment_test.go
avatar.go
avatar_test.go
branches.go
branches_test.go
commit.go
commit_status.go
commit_status_test.go
consistency.go
consistency_test.go
context.go
convert.go
error.go
error_oauth2.go
external_login_user.go
fixture_generation.go
fixture_test.go
gpg_key.go
gpg_key_add.go
gpg_key_commit_verification.go
gpg_key_common.go
gpg_key_import.go
gpg_key_test.go
gpg_key_verify.go
helper.go
helper_directory.go
helper_environment.go
index.go
index_test.go
issue.go
issue_assignees.go
issue_assignees_test.go
issue_comment.go
issue_comment_list.go
issue_comment_test.go
issue_dependency.go
issue_dependency_test.go
issue_label.go
issue_label_test.go
issue_list.go
issue_list_test.go
issue_lock.go
issue_milestone.go
issue_milestone_test.go
issue_reaction.go
issue_reaction_test.go
issue_stopwatch.go
issue_stopwatch_test.go
issue_test.go
issue_tracked_time.go
issue_tracked_time_test.go
issue_user.go
issue_user_test.go
issue_watch.go
issue_watch_test.go
issue_xref.go
issue_xref_test.go
lfs.go
lfs_lock.go
list_options.go
list_options_test.go
log.go
login_source.go
main_test.go
migrate.go
models.go
models_test.go
notification.go
notification_test.go
oauth2.go
oauth2_application.go
oauth2_application_test.go
org.go
org_team.go
org_team_test.go
org_test.go
project.go
project_board.go
project_issue.go
project_test.go
protected_tag.go
protected_tag_test.go
pull.go
pull_list.go
pull_sign.go
pull_test.go
release.go
repo.go
repo_activity.go
repo_archiver.go
repo_avatar.go
repo_branch.go
repo_collaboration.go
repo_collaboration_test.go
repo_generate.go
repo_generate_test.go
repo_indexer.go
repo_issue.go
repo_language_stats.go
repo_list.go
repo_list_test.go
repo_mirror.go
repo_permission.go
repo_permission_test.go
repo_pushmirror.go
repo_pushmirror_test.go
repo_redirect.go
repo_redirect_test.go
repo_sign.go
repo_test.go
repo_transfer.go
repo_transfer_test.go
repo_unit.go
repo_watch.go
repo_watch_test.go
review.go
review_test.go
session.go
sql_postgres_with_schema.go
ssh_key.go
ssh_key_authorized_keys.go
ssh_key_authorized_principals.go
ssh_key_deploy.go
ssh_key_fingerprint.go
ssh_key_parse.go
ssh_key_principals.go
ssh_key_test.go
star.go
star_test.go
store.go
task.go
test_fixtures.go
token.go
token_test.go
topic.go
topic_test.go
twofactor.go
u2f.go
u2f_test.go
unit.go
unit_tests.go
update.go
upload.go
user.go
user_avatar.go
user_follow.go
user_follow_test.go
user_heatmap.go
user_heatmap_test.go
user_mail.go
user_mail_test.go
user_openid.go
user_openid_test.go
user_redirect.go
user_redirect_test.go
user_test.go
userlist.go
userlist_test.go
webhook.go
webhook_test.go
wiki.go
wiki_test.go
modules
options
public
routers
services
snap
templates
tools
vendor
web_src
.air.conf
.changelog.yml
.drone.yml
.editorconfig
.eslintrc
.gitattributes
.gitignore
.golangci.yml
.ignore
.lgtm
.npmrc
.revive.toml
.stylelintrc
BSDmakefile
CHANGELOG.md
CONTRIBUTING.md
DCO
Dockerfile
Dockerfile.rootless
LICENSE
MAINTAINERS
Makefile
README.md
README_ZH.md
SECURITY.md
build.go
go.mod
go.sum
jest.config.js
main.go
package-lock.json
package.json
webpack.config.js
* Use single shared random string generation function - Replace 3 functions that do the same with 1 shared one - Use crypto/rand over math/rand for a stronger RNG - Output only alphanumerical for URL compatibilty Fixes: #15536 * use const string method * Update modules/avatar/avatar.go Co-authored-by: a1012112796 <1012112796@qq.com> Co-authored-by: a1012112796 <1012112796@qq.com>
126 lines
3.4 KiB
Go
126 lines
3.4 KiB
Go
// 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/md5"
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"encoding/base64"
|
|
"fmt"
|
|
|
|
"code.gitea.io/gitea/modules/secret"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"github.com/pquerna/otp/totp"
|
|
"golang.org/x/crypto/pbkdf2"
|
|
)
|
|
|
|
// TwoFactor represents a two-factor authentication token.
|
|
type TwoFactor struct {
|
|
ID int64 `xorm:"pk autoincr"`
|
|
UID int64 `xorm:"UNIQUE"`
|
|
Secret string
|
|
ScratchSalt string
|
|
ScratchHash string
|
|
LastUsedPasscode string `xorm:"VARCHAR(10)"`
|
|
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
|
|
}
|
|
|
|
// GenerateScratchToken recreates the scratch token the user is using.
|
|
func (t *TwoFactor) GenerateScratchToken() (string, error) {
|
|
token, err := util.RandomString(8)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
t.ScratchSalt, _ = util.RandomString(10)
|
|
t.ScratchHash = hashToken(token, t.ScratchSalt)
|
|
return token, nil
|
|
}
|
|
|
|
func hashToken(token, salt string) string {
|
|
tempHash := pbkdf2.Key([]byte(token), []byte(salt), 10000, 50, sha256.New)
|
|
return fmt.Sprintf("%x", tempHash)
|
|
}
|
|
|
|
// VerifyScratchToken verifies if the specified scratch token is valid.
|
|
func (t *TwoFactor) VerifyScratchToken(token string) bool {
|
|
if len(token) == 0 {
|
|
return false
|
|
}
|
|
tempHash := hashToken(token, t.ScratchSalt)
|
|
return subtle.ConstantTimeCompare([]byte(t.ScratchHash), []byte(tempHash)) == 1
|
|
}
|
|
|
|
func (t *TwoFactor) getEncryptionKey() []byte {
|
|
k := md5.Sum([]byte(setting.SecretKey))
|
|
return k[:]
|
|
}
|
|
|
|
// SetSecret sets the 2FA secret.
|
|
func (t *TwoFactor) SetSecret(secretString string) error {
|
|
secretBytes, err := secret.AesEncrypt(t.getEncryptionKey(), []byte(secretString))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t.Secret = base64.StdEncoding.EncodeToString(secretBytes)
|
|
return nil
|
|
}
|
|
|
|
// ValidateTOTP validates the provided passcode.
|
|
func (t *TwoFactor) ValidateTOTP(passcode string) (bool, error) {
|
|
decodedStoredSecret, err := base64.StdEncoding.DecodeString(t.Secret)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
secretBytes, err := secret.AesDecrypt(t.getEncryptionKey(), decodedStoredSecret)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
secretStr := string(secretBytes)
|
|
return totp.Validate(passcode, secretStr), nil
|
|
}
|
|
|
|
// NewTwoFactor creates a new two-factor authentication token.
|
|
func NewTwoFactor(t *TwoFactor) error {
|
|
_, err := x.Insert(t)
|
|
return err
|
|
}
|
|
|
|
// UpdateTwoFactor updates a two-factor authentication token.
|
|
func UpdateTwoFactor(t *TwoFactor) error {
|
|
_, err := x.ID(t.ID).AllCols().Update(t)
|
|
return err
|
|
}
|
|
|
|
// GetTwoFactorByUID returns the two-factor authentication token associated with
|
|
// the user, if any.
|
|
func GetTwoFactorByUID(uid int64) (*TwoFactor, error) {
|
|
twofa := &TwoFactor{}
|
|
has, err := x.Where("uid=?", uid).Get(twofa)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, ErrTwoFactorNotEnrolled{uid}
|
|
}
|
|
return twofa, nil
|
|
}
|
|
|
|
// DeleteTwoFactorByID deletes two-factor authentication token by given ID.
|
|
func DeleteTwoFactorByID(id, userID int64) error {
|
|
cnt, err := x.ID(id).Delete(&TwoFactor{
|
|
UID: userID,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
} else if cnt != 1 {
|
|
return ErrTwoFactorNotEnrolled{userID}
|
|
}
|
|
return nil
|
|
}
|