minor code style changes
This commit is contained in:
parent
4c4041a981
commit
232463e1db
11 changed files with 82 additions and 74 deletions
20
auth/api.go
20
auth/api.go
|
|
@ -12,8 +12,8 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Store defines database operations on account and token data.
|
||||
type Store interface {
|
||||
// Storer defines database operations on account and token data.
|
||||
type Storer interface {
|
||||
GetByID(id int) (*models.Account, error)
|
||||
GetByEmail(email string) (*models.Account, error)
|
||||
GetByRefreshToken(token string) (*models.Account, *models.Token, error)
|
||||
|
|
@ -23,21 +23,21 @@ type Store interface {
|
|||
PurgeExpiredToken() error
|
||||
}
|
||||
|
||||
// EmailService defines methods to send account emails.
|
||||
type EmailService interface {
|
||||
LoginToken(name, email string, c email.LoginTokenContent) error
|
||||
// Mailer defines methods to send account emails.
|
||||
type Mailer interface {
|
||||
LoginToken(name, email string, c email.ContentLoginToken) error
|
||||
}
|
||||
|
||||
// Resource implements passwordless token authentication against a database.
|
||||
type Resource struct {
|
||||
Login *LoginTokenAuth
|
||||
Token *TokenAuth
|
||||
store Store
|
||||
mailer EmailService
|
||||
store Storer
|
||||
mailer Mailer
|
||||
}
|
||||
|
||||
// NewResource returns a configured authentication resource.
|
||||
func NewResource(store Store, mailer EmailService) (*Resource, error) {
|
||||
func NewResource(store Storer, mailer Mailer) (*Resource, error) {
|
||||
loginAuth, err := NewLoginTokenAuth()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -55,7 +55,7 @@ func NewResource(store Store, mailer EmailService) (*Resource, error) {
|
|||
mailer: mailer,
|
||||
}
|
||||
|
||||
resource.Cleanup()
|
||||
resource.cleanupTicker()
|
||||
|
||||
return resource, nil
|
||||
}
|
||||
|
|
@ -75,7 +75,7 @@ func (rs *Resource) Router() *chi.Mux {
|
|||
return r
|
||||
}
|
||||
|
||||
func (rs *Resource) Cleanup() {
|
||||
func (rs *Resource) cleanupTicker() {
|
||||
ticker := time.NewTicker(time.Hour * 1)
|
||||
go func() {
|
||||
for range ticker.C {
|
||||
|
|
|
|||
|
|
@ -8,8 +8,7 @@ const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456
|
|||
|
||||
func randStringBytes(n int) string {
|
||||
buf := make([]byte, n)
|
||||
_, err := rand.Read(buf)
|
||||
if err != nil {
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -65,15 +65,15 @@ func (rs *Resource) login(w http.ResponseWriter, r *http.Request) {
|
|||
lt := rs.Login.CreateToken(acc.ID)
|
||||
|
||||
go func() {
|
||||
err := rs.mailer.LoginToken(acc.Name, acc.Email, email.LoginTokenContent{
|
||||
content := email.ContentLoginToken{
|
||||
Email: acc.Email,
|
||||
Name: acc.Name,
|
||||
URL: path.Join(rs.Login.loginURL, lt.Token),
|
||||
Token: lt.Token,
|
||||
Expiry: lt.Expiry,
|
||||
})
|
||||
if err != nil {
|
||||
log(r).WithField("module", "email").Error(err.Error())
|
||||
}
|
||||
if err := rs.mailer.LoginToken(acc.Name, acc.Email, content); err != nil {
|
||||
log(r).WithField("module", "email").Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
@ -128,6 +128,7 @@ func (rs *Resource) token(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
ua := user_agent.New(r.UserAgent())
|
||||
browser, _ := ua.Browser()
|
||||
|
||||
token := &models.Token{
|
||||
Token: uuid.NewV4().String(),
|
||||
Expiry: time.Now().Add(time.Minute * rs.Token.jwtRefreshExpiry),
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ import (
|
|||
var (
|
||||
auth *Resource
|
||||
authstore mock.AuthStore
|
||||
mailer mock.EmailService
|
||||
mailer mock.Mailer
|
||||
ts *httptest.Server
|
||||
)
|
||||
|
||||
|
|
@ -72,7 +72,7 @@ func TestAuthResource_login(t *testing.T) {
|
|||
return &a, err
|
||||
}
|
||||
|
||||
mailer.LoginTokenFn = func(n, e string, c email.LoginTokenContent) error {
|
||||
mailer.LoginTokenFn = func(n, e string, c email.ContentLoginToken) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,8 +12,8 @@ var (
|
|||
errTokenNotFound = errors.New("login token not found")
|
||||
)
|
||||
|
||||
// LoginToken is an in-memory saved token referencing an account ID and an expiry date.
|
||||
type LoginToken struct {
|
||||
// loginToken is an in-memory saved token referencing an account ID and an expiry date.
|
||||
type loginToken struct {
|
||||
Token string
|
||||
AccountID int
|
||||
Expiry time.Time
|
||||
|
|
@ -21,7 +21,7 @@ type LoginToken struct {
|
|||
|
||||
// LoginTokenAuth implements passwordless login authentication flow using temporary in-memory stored tokens.
|
||||
type LoginTokenAuth struct {
|
||||
token map[string]LoginToken
|
||||
token map[string]loginToken
|
||||
mux sync.RWMutex
|
||||
loginURL string
|
||||
loginTokenLength int
|
||||
|
|
@ -31,7 +31,7 @@ type LoginTokenAuth struct {
|
|||
// NewLoginTokenAuth configures and returns a LoginToken authentication instance.
|
||||
func NewLoginTokenAuth() (*LoginTokenAuth, error) {
|
||||
a := &LoginTokenAuth{
|
||||
token: make(map[string]LoginToken),
|
||||
token: make(map[string]loginToken),
|
||||
loginURL: viper.GetString("auth_login_url"),
|
||||
loginTokenLength: viper.GetInt("auth_login_token_length"),
|
||||
loginTokenExpiry: viper.GetDuration("auth_login_token_expiry"),
|
||||
|
|
@ -40,8 +40,8 @@ func NewLoginTokenAuth() (*LoginTokenAuth, error) {
|
|||
}
|
||||
|
||||
// CreateToken creates an in-memory login token referencing account ID. It returns a token containing a random tokenstring and expiry date.
|
||||
func (a *LoginTokenAuth) CreateToken(id int) LoginToken {
|
||||
lt := LoginToken{
|
||||
func (a *LoginTokenAuth) CreateToken(id int) loginToken {
|
||||
lt := loginToken{
|
||||
Token: randStringBytes(a.loginTokenLength),
|
||||
AccountID: id,
|
||||
Expiry: time.Now().Add(time.Minute * a.loginTokenExpiry),
|
||||
|
|
@ -61,14 +61,14 @@ func (a *LoginTokenAuth) GetAccountID(token string) (int, error) {
|
|||
return lt.AccountID, nil
|
||||
}
|
||||
|
||||
func (a *LoginTokenAuth) get(token string) (LoginToken, bool) {
|
||||
func (a *LoginTokenAuth) get(token string) (loginToken, bool) {
|
||||
a.mux.RLock()
|
||||
lt, ok := a.token[token]
|
||||
a.mux.RUnlock()
|
||||
return lt, ok
|
||||
}
|
||||
|
||||
func (a *LoginTokenAuth) add(lt LoginToken) {
|
||||
func (a *LoginTokenAuth) add(lt loginToken) {
|
||||
a.mux.Lock()
|
||||
a.token[lt.Token] = lt
|
||||
a.mux.Unlock()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue