mirror of https://github.com/gogits/gogs.git
Unknwon
8 years ago
26 changed files with 832 additions and 107 deletions
@ -0,0 +1,33 @@ |
|||||||
|
// Copyright 2017 The Gogs 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 errors |
||||||
|
|
||||||
|
import "fmt" |
||||||
|
|
||||||
|
type TwoFactorNotFound struct { |
||||||
|
UserID int64 |
||||||
|
} |
||||||
|
|
||||||
|
func IsTwoFactorNotFound(err error) bool { |
||||||
|
_, ok := err.(TwoFactorNotFound) |
||||||
|
return ok |
||||||
|
} |
||||||
|
|
||||||
|
func (err TwoFactorNotFound) Error() string { |
||||||
|
return fmt.Sprintf("two-factor authentication does not found [user_id: %d]", err.UserID) |
||||||
|
} |
||||||
|
|
||||||
|
type TwoFactorRecoveryCodeNotFound struct { |
||||||
|
Code string |
||||||
|
} |
||||||
|
|
||||||
|
func IsTwoFactorRecoveryCodeNotFound(err error) bool { |
||||||
|
_, ok := err.(TwoFactorRecoveryCodeNotFound) |
||||||
|
return ok |
||||||
|
} |
||||||
|
|
||||||
|
func (err TwoFactorRecoveryCodeNotFound) Error() string { |
||||||
|
return fmt.Sprintf("two-factor recovery code does not found [code: %s]", err.Code) |
||||||
|
} |
@ -0,0 +1,201 @@ |
|||||||
|
// Copyright 2017 The Gogs 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 ( |
||||||
|
"encoding/base64" |
||||||
|
"fmt" |
||||||
|
"strings" |
||||||
|
"time" |
||||||
|
|
||||||
|
"github.com/Unknwon/com" |
||||||
|
"github.com/go-xorm/xorm" |
||||||
|
"github.com/pquerna/otp/totp" |
||||||
|
log "gopkg.in/clog.v1" |
||||||
|
|
||||||
|
"github.com/gogits/gogs/models/errors" |
||||||
|
"github.com/gogits/gogs/pkg/setting" |
||||||
|
"github.com/gogits/gogs/pkg/tool" |
||||||
|
) |
||||||
|
|
||||||
|
// TwoFactor represents a two-factor authentication token.
|
||||||
|
type TwoFactor struct { |
||||||
|
ID int64 |
||||||
|
UserID int64 `xorm:"UNIQUE"` |
||||||
|
Secret string |
||||||
|
Created time.Time `xorm:"-"` |
||||||
|
CreatedUnix int64 |
||||||
|
} |
||||||
|
|
||||||
|
func (t *TwoFactor) BeforeInsert() { |
||||||
|
t.CreatedUnix = time.Now().Unix() |
||||||
|
} |
||||||
|
|
||||||
|
func (t *TwoFactor) AfterSet(colName string, _ xorm.Cell) { |
||||||
|
switch colName { |
||||||
|
case "created_unix": |
||||||
|
t.Created = time.Unix(t.CreatedUnix, 0).Local() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// ValidateTOTP returns true if given passcode is valid for two-factor authentication token.
|
||||||
|
// It also returns possible validation error.
|
||||||
|
func (t *TwoFactor) ValidateTOTP(passcode string) (bool, error) { |
||||||
|
secret, err := base64.StdEncoding.DecodeString(t.Secret) |
||||||
|
if err != nil { |
||||||
|
return false, fmt.Errorf("DecodeString: %v", err) |
||||||
|
} |
||||||
|
decryptSecret, err := com.AESGCMDecrypt(tool.MD5Bytes(setting.SecretKey), secret) |
||||||
|
if err != nil { |
||||||
|
return false, fmt.Errorf("AESGCMDecrypt: %v", err) |
||||||
|
} |
||||||
|
return totp.Validate(passcode, string(decryptSecret)), nil |
||||||
|
} |
||||||
|
|
||||||
|
// IsUserEnabledTwoFactor returns true if user has enabled two-factor authentication.
|
||||||
|
func IsUserEnabledTwoFactor(userID int64) bool { |
||||||
|
has, err := x.Where("user_id = ?", userID).Get(new(TwoFactor)) |
||||||
|
if err != nil { |
||||||
|
log.Error(2, "IsUserEnabledTwoFactor [user_id: %d]: %v", userID, err) |
||||||
|
} |
||||||
|
return has |
||||||
|
} |
||||||
|
|
||||||
|
func generateRecoveryCodes(userID int64) ([]*TwoFactorRecoveryCode, error) { |
||||||
|
recoveryCodes := make([]*TwoFactorRecoveryCode, 10) |
||||||
|
for i := 0; i < 10; i++ { |
||||||
|
code, err := tool.GetRandomString(10) |
||||||
|
if err != nil { |
||||||
|
return nil, fmt.Errorf("GetRandomString: %v", err) |
||||||
|
} |
||||||
|
recoveryCodes[i] = &TwoFactorRecoveryCode{ |
||||||
|
UserID: userID, |
||||||
|
Code: strings.ToLower(code[:5] + "-" + code[5:]), |
||||||
|
} |
||||||
|
} |
||||||
|
return recoveryCodes, nil |
||||||
|
} |
||||||
|
|
||||||
|
// NewTwoFactor creates a new two-factor authentication token and recovery codes for given user.
|
||||||
|
func NewTwoFactor(userID int64, secret string) error { |
||||||
|
t := &TwoFactor{ |
||||||
|
UserID: userID, |
||||||
|
} |
||||||
|
|
||||||
|
// Encrypt secret
|
||||||
|
encryptSecret, err := com.AESGCMEncrypt(tool.MD5Bytes(setting.SecretKey), []byte(secret)) |
||||||
|
if err != nil { |
||||||
|
return fmt.Errorf("AESGCMEncrypt: %v", err) |
||||||
|
} |
||||||
|
t.Secret = base64.StdEncoding.EncodeToString(encryptSecret) |
||||||
|
|
||||||
|
recoveryCodes, err := generateRecoveryCodes(userID) |
||||||
|
if err != nil { |
||||||
|
return fmt.Errorf("generateRecoveryCodes: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
sess := x.NewSession() |
||||||
|
defer sess.Close() |
||||||
|
if err = sess.Begin(); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if _, err = sess.Insert(t); err != nil { |
||||||
|
return fmt.Errorf("insert two-factor: %v", err) |
||||||
|
} else if _, err = sess.Insert(recoveryCodes); err != nil { |
||||||
|
return fmt.Errorf("insert recovery codes: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
return sess.Commit() |
||||||
|
} |
||||||
|
|
||||||
|
// GetTwoFactorByUserID returns two-factor authentication token of given user.
|
||||||
|
func GetTwoFactorByUserID(userID int64) (*TwoFactor, error) { |
||||||
|
t := new(TwoFactor) |
||||||
|
has, err := x.Where("user_id = ?", userID).Get(t) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} else if !has { |
||||||
|
return nil, errors.TwoFactorNotFound{userID} |
||||||
|
} |
||||||
|
|
||||||
|
return t, nil |
||||||
|
} |
||||||
|
|
||||||
|
// DeleteTwoFactor removes two-factor authentication token and recovery codes of given user.
|
||||||
|
func DeleteTwoFactor(userID int64) (err error) { |
||||||
|
sess := x.NewSession() |
||||||
|
defer sess.Close() |
||||||
|
if err = sess.Begin(); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if _, err = sess.Where("user_id = ?", userID).Delete(new(TwoFactor)); err != nil { |
||||||
|
return fmt.Errorf("delete two-factor: %v", err) |
||||||
|
} else if err = deleteRecoveryCodesByUserID(sess, userID); err != nil { |
||||||
|
return fmt.Errorf("deleteRecoveryCodesByUserID: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
return sess.Commit() |
||||||
|
} |
||||||
|
|
||||||
|
// TwoFactorRecoveryCode represents a two-factor authentication recovery code.
|
||||||
|
type TwoFactorRecoveryCode struct { |
||||||
|
ID int64 |
||||||
|
UserID int64 |
||||||
|
Code string `xorm:"VARCHAR(11)"` |
||||||
|
IsUsed bool |
||||||
|
} |
||||||
|
|
||||||
|
// GetRecoveryCodesByUserID returns all recovery codes of given user.
|
||||||
|
func GetRecoveryCodesByUserID(userID int64) ([]*TwoFactorRecoveryCode, error) { |
||||||
|
recoveryCodes := make([]*TwoFactorRecoveryCode, 0, 10) |
||||||
|
return recoveryCodes, x.Where("user_id = ?", userID).Find(&recoveryCodes) |
||||||
|
} |
||||||
|
|
||||||
|
func deleteRecoveryCodesByUserID(e Engine, userID int64) error { |
||||||
|
_, err := e.Where("user_id = ?", userID).Delete(new(TwoFactorRecoveryCode)) |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
// RegenerateRecoveryCodes regenerates new set of recovery codes for given user.
|
||||||
|
func RegenerateRecoveryCodes(userID int64) error { |
||||||
|
recoveryCodes, err := generateRecoveryCodes(userID) |
||||||
|
if err != nil { |
||||||
|
return fmt.Errorf("generateRecoveryCodes: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
sess := x.NewSession() |
||||||
|
defer sess.Close() |
||||||
|
if err = sess.Begin(); err != nil { |
||||||
|
return err |
||||||
|
} |
||||||
|
|
||||||
|
if err = deleteRecoveryCodesByUserID(sess, userID); err != nil { |
||||||
|
return fmt.Errorf("deleteRecoveryCodesByUserID: %v", err) |
||||||
|
} else if _, err = sess.Insert(recoveryCodes); err != nil { |
||||||
|
return fmt.Errorf("insert new recovery codes: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
return sess.Commit() |
||||||
|
} |
||||||
|
|
||||||
|
// UseRecoveryCode validates recovery code of given user and marks it is used if valid.
|
||||||
|
func UseRecoveryCode(userID int64, code string) error { |
||||||
|
recoveryCode := new(TwoFactorRecoveryCode) |
||||||
|
has, err := x.Where("code = ?", code).And("is_used = ?", false).Get(recoveryCode) |
||||||
|
if err != nil { |
||||||
|
return fmt.Errorf("get unused code: %v", err) |
||||||
|
} else if !has { |
||||||
|
return errors.TwoFactorRecoveryCodeNotFound{code} |
||||||
|
} |
||||||
|
|
||||||
|
recoveryCode.IsUsed = true |
||||||
|
if _, err = x.Id(recoveryCode.ID).Cols("is_used").Update(recoveryCode); err != nil { |
||||||
|
return fmt.Errorf("mark code as used: %v", err) |
||||||
|
} |
||||||
|
|
||||||
|
return nil |
||||||
|
} |
File diff suppressed because one or more lines are too long
@ -0,0 +1,28 @@ |
|||||||
|
{{template "base/head" .}} |
||||||
|
<div class="user signin two-factor"> |
||||||
|
<div class="ui middle very relaxed page grid"> |
||||||
|
<div class="column"> |
||||||
|
<form class="ui form" action="{{.Link}}" method="post"> |
||||||
|
{{.CsrfTokenHtml}} |
||||||
|
<h3 class="ui top attached center header"> |
||||||
|
{{.i18n.Tr "auth.login_two_factor"}} |
||||||
|
</h3> |
||||||
|
<div class="ui attached segment"> |
||||||
|
{{template "base/alert" .}} |
||||||
|
<div class="required field"> |
||||||
|
<label for="passcode">{{.i18n.Tr "auth.login_two_factor_passcode"}}</label> |
||||||
|
<div class="ui fluid input"> |
||||||
|
<input id="passcode" name="passcode" autofocus required> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
|
||||||
|
<button class="ui fluid green button">{{.i18n.Tr "settings.two_factor_verify"}}</button> |
||||||
|
</div> |
||||||
|
<p> |
||||||
|
<a href="{{AppSubUrl}}/user/login/two_factor_recovery_code">{{.i18n.Tr "auth.login_two_factor_enter_recovery_code"}}</a> |
||||||
|
</p> |
||||||
|
</form> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
{{template "base/footer" .}} |
@ -0,0 +1,28 @@ |
|||||||
|
{{template "base/head" .}} |
||||||
|
<div class="user signin two-factor"> |
||||||
|
<div class="ui middle very relaxed page grid"> |
||||||
|
<div class="column"> |
||||||
|
<form class="ui form" action="{{.Link}}" method="post"> |
||||||
|
{{.CsrfTokenHtml}} |
||||||
|
<h3 class="ui top attached center header"> |
||||||
|
{{.i18n.Tr "auth.login_two_factor_recovery"}} |
||||||
|
</h3> |
||||||
|
<div class="ui attached segment"> |
||||||
|
{{template "base/alert" .}} |
||||||
|
<div class="required field"> |
||||||
|
<label for="recovery_code">{{.i18n.Tr "auth.login_two_factor_recovery_code"}}</label> |
||||||
|
<div class="ui fluid input"> |
||||||
|
<input id="recovery_code" name="recovery_code" autofocus required> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
|
||||||
|
<button class="ui fluid green button">{{.i18n.Tr "settings.two_factor_verify"}}</button> |
||||||
|
</div> |
||||||
|
<p> |
||||||
|
<a href="{{AppSubUrl}}/user/login/two_factor">{{.i18n.Tr "auth.login_two_factor_enter_passcode"}}</a> |
||||||
|
</p> |
||||||
|
</form> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
{{template "base/footer" .}} |
@ -0,0 +1,51 @@ |
|||||||
|
{{template "base/head" .}} |
||||||
|
<div class="user settings security"> |
||||||
|
<div class="ui container"> |
||||||
|
<div class="ui grid"> |
||||||
|
{{template "user/settings/navbar" .}} |
||||||
|
<div class="twelve wide column content"> |
||||||
|
{{template "base/alert" .}} |
||||||
|
<h4 class="ui top attached header"> |
||||||
|
{{.i18n.Tr "settings.two_factor"}} |
||||||
|
</h4> |
||||||
|
<div class="ui attached segment two-factor"> |
||||||
|
<p class="text bold"> |
||||||
|
{{.i18n.Tr "settings.two_factor_status"}} |
||||||
|
{{if .TwoFactor}} |
||||||
|
<span class="text green">{{.i18n.Tr "settings.two_factor_on"}} <i class="octicon octicon-check"></i></span> |
||||||
|
<button class="ui right mini red toggle button delete-button" data-url="{{$.Link}}/two_factor_disable">{{.i18n.Tr "settings.two_factor_disable"}}</button> |
||||||
|
{{else}} |
||||||
|
<span class="text red">{{.i18n.Tr "settings.two_factor_off"}} <i class="octicon octicon-x"></i></span> |
||||||
|
<a class="ui right mini green toggle button" href="{{AppSubUrl}}/user/settings/security/two_factor_enable">{{.i18n.Tr "settings.two_factor_enable"}}</a> |
||||||
|
{{end}} |
||||||
|
</p> |
||||||
|
</div> |
||||||
|
{{if .TwoFactor}} |
||||||
|
<br> |
||||||
|
<p>{{.i18n.Tr "settings.two_factor_view_recovery_codes" AppSubUrl "/user/settings/security/two_factor_recovery_codes" | Safe}}</p> |
||||||
|
{{end}} |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
|
||||||
|
<div class="ui small basic delete modal"> |
||||||
|
<div class="ui icon header"> |
||||||
|
<i class="trash icon"></i> |
||||||
|
{{.i18n.Tr "settings.two_factor_disable_title"}} |
||||||
|
</div> |
||||||
|
<div class="content"> |
||||||
|
<p>{{.i18n.Tr "settings.two_factor_disable_desc"}}</p> |
||||||
|
</div> |
||||||
|
<div class="actions"> |
||||||
|
<div class="ui red basic inverted cancel button"> |
||||||
|
<i class="remove icon"></i> |
||||||
|
{{.i18n.Tr "modal.no"}} |
||||||
|
</div> |
||||||
|
<div class="ui green basic inverted ok button"> |
||||||
|
<i class="checkmark icon"></i> |
||||||
|
{{.i18n.Tr "modal.yes"}} |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
{{template "base/footer" .}} |
@ -0,0 +1,28 @@ |
|||||||
|
{{template "base/head" .}} |
||||||
|
<div class="user settings security two-factor"> |
||||||
|
<div class="ui container"> |
||||||
|
<div class="ui grid"> |
||||||
|
{{template "user/settings/navbar" .}} |
||||||
|
<div class="twelve wide column content"> |
||||||
|
{{template "base/alert" .}} |
||||||
|
<h4 class="ui top attached header"> |
||||||
|
{{.i18n.Tr "settings.two_factor_enable_title"}} |
||||||
|
</h4> |
||||||
|
<div class="ui attached segment"> |
||||||
|
<div>{{.i18n.Tr "settings.two_factor_scan_qr"}}</div> |
||||||
|
<img src="{{.QRCode}}" alt="{{.TwoFactorSecret}}"> |
||||||
|
<p>{{.i18n.Tr "settings.two_factor_or_enter_secret"}} <b>{{.TwoFactorSecret}}</b></p> |
||||||
|
<form class="ui form" method="post"> |
||||||
|
{{.CsrfTokenHtml}} |
||||||
|
<div class="required inline field"> |
||||||
|
<span>{{.i18n.Tr "settings.two_factor_then_enter_passcode"}}</span> |
||||||
|
<input class="ui input" name="passcode" autocomplete="off" autofocus required> |
||||||
|
</div> |
||||||
|
<button class="ui green button">{{.i18n.Tr "settings.two_factor_verify"}}</button> |
||||||
|
</form> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
{{template "base/footer" .}} |
@ -0,0 +1,36 @@ |
|||||||
|
{{template "base/head" .}} |
||||||
|
<div class="user settings security two-factor"> |
||||||
|
<div class="ui container"> |
||||||
|
<div class="ui grid"> |
||||||
|
{{template "user/settings/navbar" .}} |
||||||
|
<div class="twelve wide column content"> |
||||||
|
{{template "base/alert" .}} |
||||||
|
<h4 class="ui top attached header"> |
||||||
|
{{.i18n.Tr "settings.two_factor_recovery_codes_title"}} |
||||||
|
</h4> |
||||||
|
<div class="ui attached segment"> |
||||||
|
<p>{{.i18n.Tr "settings.two_factor_recovery_codes_desc" | Safe}}</p> |
||||||
|
<ul class="ui list"> |
||||||
|
{{range .RecoveryCodes}} |
||||||
|
<li class="item"> |
||||||
|
<code> |
||||||
|
{{if .IsUsed}} |
||||||
|
<del>{{.Code}}</del> |
||||||
|
{{else}} |
||||||
|
{{.Code}} |
||||||
|
{{end}} |
||||||
|
</code> |
||||||
|
</li> |
||||||
|
{{end}} |
||||||
|
</ul> |
||||||
|
|
||||||
|
<form class="ui form" method="post"> |
||||||
|
{{.CsrfTokenHtml}} |
||||||
|
<button class="ui blue button">{{.i18n.Tr "settings.two_factor_regenerate_recovery_codes"}}</button> |
||||||
|
</form> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
{{template "base/footer" .}} |
Loading…
Reference in new issue