mirror of
https://github.com/netbirdio/netbird.git
synced 2025-01-25 07:19:05 +01:00
Refactor pat to support mysql
Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
parent
525019b5ed
commit
79f94dd0bb
@ -1134,7 +1134,7 @@ func (am *DefaultAccountManager) MarkPATUsed(ctx context.Context, tokenID string
|
|||||||
return fmt.Errorf("token not found")
|
return fmt.Errorf("token not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
pat.LastUsed = time.Now().UTC()
|
pat.LastUsed = util.ToPtr(time.Now().UTC())
|
||||||
|
|
||||||
return am.Store.SaveAccount(ctx, account)
|
return am.Store.SaveAccount(ctx, account)
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package users
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
|
||||||
@ -166,17 +165,13 @@ func (h *patHandler) deleteToken(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func toPATResponse(pat *types.PersonalAccessToken) *api.PersonalAccessToken {
|
func toPATResponse(pat *types.PersonalAccessToken) *api.PersonalAccessToken {
|
||||||
var lastUsed *time.Time
|
|
||||||
if !pat.LastUsed.IsZero() {
|
|
||||||
lastUsed = &pat.LastUsed
|
|
||||||
}
|
|
||||||
return &api.PersonalAccessToken{
|
return &api.PersonalAccessToken{
|
||||||
CreatedAt: pat.CreatedAt,
|
CreatedAt: pat.CreatedAt,
|
||||||
CreatedBy: pat.CreatedBy,
|
CreatedBy: pat.CreatedBy,
|
||||||
Name: pat.Name,
|
Name: pat.Name,
|
||||||
ExpirationDate: pat.ExpirationDate,
|
ExpirationDate: pat.ExpirationTime(),
|
||||||
Id: pat.ID,
|
Id: pat.ID,
|
||||||
LastUsed: lastUsed,
|
LastUsed: pat.LastUsed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ func (m *AuthMiddleware) checkPATFromRequest(w http.ResponseWriter, r *http.Requ
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid Token: %w", err)
|
return fmt.Errorf("invalid Token: %w", err)
|
||||||
}
|
}
|
||||||
if time.Now().After(pat.ExpirationDate) {
|
if time.Now().After(pat.ExpirationTime()) {
|
||||||
return fmt.Errorf("token expired")
|
return fmt.Errorf("token expired")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
b "github.com/hashicorp/go-secure-stdlib/base62"
|
b "github.com/hashicorp/go-secure-stdlib/base62"
|
||||||
|
"github.com/netbirdio/netbird/management/server/util"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
|
|
||||||
"github.com/netbirdio/netbird/base62"
|
"github.com/netbirdio/netbird/base62"
|
||||||
@ -31,11 +32,11 @@ type PersonalAccessToken struct {
|
|||||||
UserID string `gorm:"index"`
|
UserID string `gorm:"index"`
|
||||||
Name string
|
Name string
|
||||||
HashedToken string
|
HashedToken string
|
||||||
ExpirationDate time.Time
|
ExpirationDate *time.Time
|
||||||
// scope could be added in future
|
// scope could be added in future
|
||||||
CreatedBy string
|
CreatedBy string
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
LastUsed time.Time
|
LastUsed *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *PersonalAccessToken) Copy() *PersonalAccessToken {
|
func (t *PersonalAccessToken) Copy() *PersonalAccessToken {
|
||||||
@ -50,6 +51,22 @@ func (t *PersonalAccessToken) Copy() *PersonalAccessToken {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExpirationTime returns the expiration time of the token.
|
||||||
|
func (t *PersonalAccessToken) ExpirationTime() time.Time {
|
||||||
|
if t.ExpirationDate != nil {
|
||||||
|
return *t.ExpirationDate
|
||||||
|
}
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// LastUsedTime returns the last time the token was used.
|
||||||
|
func (t *PersonalAccessToken) LastUsedTime() time.Time {
|
||||||
|
if t.LastUsed != nil {
|
||||||
|
return *t.LastUsed
|
||||||
|
}
|
||||||
|
return time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
// PersonalAccessTokenGenerated holds the new PersonalAccessToken and the plain text version of it
|
// PersonalAccessTokenGenerated holds the new PersonalAccessToken and the plain text version of it
|
||||||
type PersonalAccessTokenGenerated struct {
|
type PersonalAccessTokenGenerated struct {
|
||||||
PlainToken string
|
PlainToken string
|
||||||
@ -69,10 +86,9 @@ func CreateNewPAT(name string, expirationInDays int, createdBy string) (*Persona
|
|||||||
ID: xid.New().String(),
|
ID: xid.New().String(),
|
||||||
Name: name,
|
Name: name,
|
||||||
HashedToken: hashedToken,
|
HashedToken: hashedToken,
|
||||||
ExpirationDate: currentTime.AddDate(0, 0, expirationInDays),
|
ExpirationDate: util.ToPtr(currentTime.AddDate(0, 0, expirationInDays)),
|
||||||
CreatedBy: createdBy,
|
CreatedBy: createdBy,
|
||||||
CreatedAt: currentTime,
|
CreatedAt: currentTime,
|
||||||
LastUsed: time.Time{},
|
|
||||||
},
|
},
|
||||||
PlainToken: plainToken,
|
PlainToken: plainToken,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -142,11 +142,6 @@ func (u *User) ToUserInfo(userData *idp.UserData, settings *Settings) (*UserInfo
|
|||||||
}
|
}
|
||||||
|
|
||||||
if userData == nil {
|
if userData == nil {
|
||||||
var lastLogin time.Time
|
|
||||||
if u.LastLogin != nil {
|
|
||||||
lastLogin = *u.LastLogin
|
|
||||||
}
|
|
||||||
|
|
||||||
return &UserInfo{
|
return &UserInfo{
|
||||||
ID: u.Id,
|
ID: u.Id,
|
||||||
Email: "",
|
Email: "",
|
||||||
@ -156,7 +151,7 @@ func (u *User) ToUserInfo(userData *idp.UserData, settings *Settings) (*UserInfo
|
|||||||
Status: string(UserStatusActive),
|
Status: string(UserStatusActive),
|
||||||
IsServiceUser: u.IsServiceUser,
|
IsServiceUser: u.IsServiceUser,
|
||||||
IsBlocked: u.Blocked,
|
IsBlocked: u.Blocked,
|
||||||
LastLogin: lastLogin,
|
LastLogin: u.LastLoginTime(),
|
||||||
Issued: u.Issued,
|
Issued: u.Issued,
|
||||||
Permissions: UserPermissions{
|
Permissions: UserPermissions{
|
||||||
DashboardView: dashboardViewPermissions,
|
DashboardView: dashboardViewPermissions,
|
||||||
@ -172,11 +167,6 @@ func (u *User) ToUserInfo(userData *idp.UserData, settings *Settings) (*UserInfo
|
|||||||
userStatus = UserStatusInvited
|
userStatus = UserStatusInvited
|
||||||
}
|
}
|
||||||
|
|
||||||
lastLogin := time.Time{}
|
|
||||||
if u.LastLogin != nil {
|
|
||||||
lastLogin = *u.LastLogin
|
|
||||||
}
|
|
||||||
|
|
||||||
return &UserInfo{
|
return &UserInfo{
|
||||||
ID: u.Id,
|
ID: u.Id,
|
||||||
Email: userData.Email,
|
Email: userData.Email,
|
||||||
@ -186,7 +176,7 @@ func (u *User) ToUserInfo(userData *idp.UserData, settings *Settings) (*UserInfo
|
|||||||
Status: string(userStatus),
|
Status: string(userStatus),
|
||||||
IsServiceUser: u.IsServiceUser,
|
IsServiceUser: u.IsServiceUser,
|
||||||
IsBlocked: u.Blocked,
|
IsBlocked: u.Blocked,
|
||||||
LastLogin: lastLogin,
|
LastLogin: u.LastLoginTime(),
|
||||||
Issued: u.Issued,
|
Issued: u.Issued,
|
||||||
Permissions: UserPermissions{
|
Permissions: UserPermissions{
|
||||||
DashboardView: dashboardViewPermissions,
|
DashboardView: dashboardViewPermissions,
|
||||||
|
Loading…
Reference in New Issue
Block a user