Missing from previous commit
This commit is contained in:
parent
6a2b213787
commit
6f16289b41
1 changed files with 29 additions and 0 deletions
29
auth-new/accessTokens.go
Normal file
29
auth-new/accessTokens.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.mstar.dev/mstar/goutils/other"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/dbgen"
|
||||
"git.mstar.dev/mstar/linstrom/storage-new/models"
|
||||
)
|
||||
|
||||
// Check whether a given access token is valid (exists and hasn't expired).
|
||||
// If it is, returns the user it belongs to
|
||||
func (a *Authenticator) IsValidAccessToken(token string) (*models.User, error) {
|
||||
dbToken, err := dbgen.AccessToken.GetTokenIfValid(token)
|
||||
switch err {
|
||||
case nil:
|
||||
if dbToken.ExpiresAt.Before(time.Now()) {
|
||||
return nil, ErrTokenExpired
|
||||
} else {
|
||||
return &dbToken.User, nil
|
||||
}
|
||||
case gorm.ErrRecordNotFound:
|
||||
return nil, ErrTokenNotFound
|
||||
default:
|
||||
return nil, other.Error("auth", "failed to check for token", err)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue