mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-20 09:47:49 +02:00
refactor to use name instead of description
This commit is contained in:
parent
9e74f30d2f
commit
c65a934107
@ -1245,7 +1245,7 @@ func TestAccount_Copy(t *testing.T) {
|
|||||||
PATs: map[string]*PersonalAccessToken{
|
PATs: map[string]*PersonalAccessToken{
|
||||||
"pat1": {
|
"pat1": {
|
||||||
ID: "pat1",
|
ID: "pat1",
|
||||||
Description: "First PAT",
|
Name: "First PAT",
|
||||||
HashedToken: "SoMeHaShEdToKeN",
|
HashedToken: "SoMeHaShEdToKeN",
|
||||||
ExpirationDate: time.Now().AddDate(0, 0, 7),
|
ExpirationDate: time.Now().AddDate(0, 0, 7),
|
||||||
CreatedBy: "user1",
|
CreatedBy: "user1",
|
||||||
|
@ -292,12 +292,9 @@ components:
|
|||||||
id:
|
id:
|
||||||
description: ID of a token
|
description: ID of a token
|
||||||
type: string
|
type: string
|
||||||
description:
|
name:
|
||||||
description: Description of the token
|
description: Name of the token
|
||||||
type: string
|
type: string
|
||||||
# hashed_token:
|
|
||||||
# description: Hashed representation of the token
|
|
||||||
# type: string
|
|
||||||
expiration_date:
|
expiration_date:
|
||||||
description: Date the token expires
|
description: Date the token expires
|
||||||
type: string
|
type: string
|
||||||
@ -315,8 +312,7 @@ components:
|
|||||||
format: date-time
|
format: date-time
|
||||||
required:
|
required:
|
||||||
- id
|
- id
|
||||||
- description
|
- name
|
||||||
# - hashed_token
|
|
||||||
- expiration_date
|
- expiration_date
|
||||||
- created_by
|
- created_by
|
||||||
- created_at
|
- created_at
|
||||||
@ -324,14 +320,14 @@ components:
|
|||||||
PersonalAccessTokenRequest:
|
PersonalAccessTokenRequest:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
description:
|
name:
|
||||||
description: Description of the token
|
description: Name of the token
|
||||||
type: string
|
type: string
|
||||||
expires_in:
|
expires_in:
|
||||||
description: Expiration in days
|
description: Expiration in days
|
||||||
type: integer
|
type: integer
|
||||||
required:
|
required:
|
||||||
- description
|
- name
|
||||||
- expires_in
|
- expires_in
|
||||||
GroupMinimum:
|
GroupMinimum:
|
||||||
type: object
|
type: object
|
||||||
|
@ -387,9 +387,6 @@ type PersonalAccessToken struct {
|
|||||||
// CreatedBy User ID of the user who created the token
|
// CreatedBy User ID of the user who created the token
|
||||||
CreatedBy string `json:"created_by"`
|
CreatedBy string `json:"created_by"`
|
||||||
|
|
||||||
// Description Description of the token
|
|
||||||
Description string `json:"description"`
|
|
||||||
|
|
||||||
// ExpirationDate Date the token expires
|
// ExpirationDate Date the token expires
|
||||||
ExpirationDate time.Time `json:"expiration_date"`
|
ExpirationDate time.Time `json:"expiration_date"`
|
||||||
|
|
||||||
@ -398,15 +395,18 @@ type PersonalAccessToken struct {
|
|||||||
|
|
||||||
// LastUsed Date the token was last used
|
// LastUsed Date the token was last used
|
||||||
LastUsed time.Time `json:"last_used"`
|
LastUsed time.Time `json:"last_used"`
|
||||||
|
|
||||||
|
// Name Name of the token
|
||||||
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PersonalAccessTokenRequest defines model for PersonalAccessTokenRequest.
|
// PersonalAccessTokenRequest defines model for PersonalAccessTokenRequest.
|
||||||
type PersonalAccessTokenRequest struct {
|
type PersonalAccessTokenRequest struct {
|
||||||
// Description Description of the token
|
|
||||||
Description string `json:"description"`
|
|
||||||
|
|
||||||
// ExpiresIn Expiration in days
|
// ExpiresIn Expiration in days
|
||||||
ExpiresIn int `json:"expires_in"`
|
ExpiresIn int `json:"expires_in"`
|
||||||
|
|
||||||
|
// Name Name of the token
|
||||||
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Policy defines model for Policy.
|
// Policy defines model for Policy.
|
||||||
|
@ -30,11 +30,6 @@ func NewPATsHandler(accountManager server.AccountManager, authCfg AuthCfg) *PATH
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *PATHandler) GetAllTokens(w http.ResponseWriter, r *http.Request) {
|
func (h *PATHandler) GetAllTokens(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
claims := h.claimsExtractor.FromRequestContext(r)
|
claims := h.claimsExtractor.FromRequestContext(r)
|
||||||
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -62,11 +57,6 @@ func (h *PATHandler) GetAllTokens(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *PATHandler) GetToken(w http.ResponseWriter, r *http.Request) {
|
func (h *PATHandler) GetToken(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
claims := h.claimsExtractor.FromRequestContext(r)
|
claims := h.claimsExtractor.FromRequestContext(r)
|
||||||
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -96,11 +86,6 @@ func (h *PATHandler) GetToken(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *PATHandler) CreateToken(w http.ResponseWriter, r *http.Request) {
|
func (h *PATHandler) CreateToken(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPut {
|
|
||||||
util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
claims := h.claimsExtractor.FromRequestContext(r)
|
claims := h.claimsExtractor.FromRequestContext(r)
|
||||||
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -126,7 +111,7 @@ func (h *PATHandler) CreateToken(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pat, plainToken, err := server.CreateNewPAT(req.Description, req.ExpiresIn, user.Id)
|
pat, plainToken, err := server.CreateNewPAT(req.Name, req.ExpiresIn, user.Id)
|
||||||
err = h.accountManager.AddPATToUser(account.Id, userID, pat)
|
err = h.accountManager.AddPATToUser(account.Id, userID, pat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.WriteError(err, w)
|
util.WriteError(err, w)
|
||||||
@ -137,11 +122,6 @@ func (h *PATHandler) CreateToken(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *PATHandler) DeleteToken(w http.ResponseWriter, r *http.Request) {
|
func (h *PATHandler) DeleteToken(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodDelete {
|
|
||||||
util.WriteErrorResponse("wrong HTTP method", http.StatusMethodNotAllowed, w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
claims := h.claimsExtractor.FromRequestContext(r)
|
claims := h.claimsExtractor.FromRequestContext(r)
|
||||||
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -179,7 +159,7 @@ func toPATResponse(pat *server.PersonalAccessToken) *api.PersonalAccessToken {
|
|||||||
return &api.PersonalAccessToken{
|
return &api.PersonalAccessToken{
|
||||||
CreatedAt: pat.CreatedAt,
|
CreatedAt: pat.CreatedAt,
|
||||||
CreatedBy: pat.CreatedBy,
|
CreatedBy: pat.CreatedBy,
|
||||||
Description: pat.Description,
|
Name: pat.Name,
|
||||||
ExpirationDate: pat.ExpirationDate,
|
ExpirationDate: pat.ExpirationDate,
|
||||||
Id: pat.ID,
|
Id: pat.ID,
|
||||||
LastUsed: pat.LastUsed,
|
LastUsed: pat.LastUsed,
|
||||||
|
@ -25,7 +25,7 @@ const (
|
|||||||
// PersonalAccessToken holds all information about a PAT including a hashed version of it for verification
|
// PersonalAccessToken holds all information about a PAT including a hashed version of it for verification
|
||||||
type PersonalAccessToken struct {
|
type PersonalAccessToken struct {
|
||||||
ID string
|
ID string
|
||||||
Description 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
|
||||||
@ -36,7 +36,7 @@ type PersonalAccessToken struct {
|
|||||||
|
|
||||||
// CreateNewPAT will generate a new PersonalAccessToken that can be assigned to a User.
|
// CreateNewPAT will generate a new PersonalAccessToken that can be assigned to a User.
|
||||||
// Additionally, it will return the token in plain text once, to give to the user and only save a hashed version
|
// Additionally, it will return the token in plain text once, to give to the user and only save a hashed version
|
||||||
func CreateNewPAT(description string, expirationInDays int, createdBy string) (*PersonalAccessToken, string, error) {
|
func CreateNewPAT(name string, expirationInDays int, createdBy string) (*PersonalAccessToken, string, error) {
|
||||||
hashedToken, plainToken, err := generateNewToken()
|
hashedToken, plainToken, err := generateNewToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
@ -44,7 +44,7 @@ func CreateNewPAT(description string, expirationInDays int, createdBy string) (*
|
|||||||
currentTime := time.Now().UTC()
|
currentTime := time.Now().UTC()
|
||||||
return &PersonalAccessToken{
|
return &PersonalAccessToken{
|
||||||
ID: xid.New().String(),
|
ID: xid.New().String(),
|
||||||
Description: description,
|
Name: name,
|
||||||
HashedToken: hashedToken,
|
HashedToken: hashedToken,
|
||||||
ExpirationDate: currentTime.AddDate(0, 0, expirationInDays),
|
ExpirationDate: currentTime.AddDate(0, 0, expirationInDays),
|
||||||
CreatedBy: createdBy,
|
CreatedBy: createdBy,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user