forked from gogs/gogs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial addition of two-factor authentication support
- Loading branch information
1 parent
0c9a616
commit 5cd2997
Showing
14 changed files
with
1,230 additions
and
911 deletions.
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
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
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,48 @@ | ||
// Copyright 2016 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 ( | ||
"time" | ||
) | ||
|
||
// TwoFactor represents a stored two-factor authentication secret. | ||
type TwoFactor struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
UID int64 `xorm:"UNIQUE"` | ||
Secret string `xorm:"UNIQUE VARCHAR(16)"` | ||
Created time.Time `xorm:"CREATED"` | ||
Updated time.Time | ||
} | ||
|
||
// NewTwoFactor creates a new two factor token. | ||
func NewTwoFactor(t *TwoFactor) error { | ||
_, err := x.Insert(t) | ||
return err | ||
} | ||
|
||
// GetTwoFactorByUID returns two factor token by given user ID. | ||
func GetTwoFactorByUID(uid int64) (*TwoFactor, error) { | ||
t := &TwoFactor{UID: uid} | ||
has, err := x.Get(t) | ||
if err != nil { | ||
return nil, err | ||
} else if !has { | ||
return nil, ErrTwoFactorNotExist{uid} | ||
} | ||
return t, nil | ||
} | ||
|
||
// UpdateTwoFactorupdates information of two factor token. | ||
func UpdateTwoFactor(t *TwoFactor) error { | ||
_, err := x.Id(t.ID).AllCols().Update(t) | ||
return err | ||
} | ||
|
||
// DeleteTwoFactorByID deletes two faactor token by given ID. | ||
func DeleteTwoFactorByID(id int64) error { | ||
_, err := x.Id(id).Delete(new(TwoFactor)) | ||
return err | ||
} |
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,53 @@ | ||
// Copyright 2016 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 ( | ||
"time" | ||
) | ||
|
||
// TwoFactorBackup represents a stored two-factor authentication backup code. | ||
type TwoFactorBackup struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
UID int64 `xorm:"UNIQUE"` | ||
Code string `xorm:"UNIQUE VARCHAR(16)"` | ||
Created time.Time `xorm:"CREATED"` | ||
Updated time.Time | ||
Burnt bool | ||
} | ||
|
||
// NewTwoFactorBackup creates a new two factor token. | ||
func NewTwoFactorBackup(t *TwoFactorBackup) error { | ||
_, err := x.Insert(t) | ||
return err | ||
} | ||
|
||
// BurnTwoFactorBackupCode will try to "burn" a two-factor authentication backup code. | ||
func BurnTwoFactorBackupCode(uid int64, code string) error { | ||
t := &TwoFactorBackup{UID: uid, Code: code, Burnt: false} | ||
has, err := x.Get(t) | ||
if err != nil { | ||
return err | ||
} else if !has { | ||
return nil, ErrTwoFactorNotExist{uid} | ||
} | ||
|
||
// Now we burn the backup code | ||
t.Burnt = true | ||
return UpdateTwoFactorBackup(t) | ||
} | ||
|
||
// UpdateTwoFactorBackup updates information of two factor token. | ||
func UpdateTwoFactorBackup(t *TwoFactorBackup) error { | ||
_, err := x.Id(t.ID).AllCols().Update(t) | ||
return err | ||
} | ||
|
||
// DeleteTwoFactorCodesFromUser deletes two factor authentication backup codes from a user. | ||
func DeleteTwoFactorCodesFromUser(uid int64) error { | ||
deleteBy := &TwoFactorBackup{UID: uid} | ||
_, err := x.Delete(deleteBy) | ||
return err | ||
} |
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.