|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package common_db |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/uuid" |
| 13 | + "google.golang.org/grpc/codes" |
| 14 | + "google.golang.org/grpc/status" |
| 15 | + "gorm.io/gorm" |
| 16 | +) |
| 17 | + |
| 18 | +type PersonalAccessToken struct { |
| 19 | + ID uuid.UUID `gorm:"primary_key;column:id;type:varchar;size:255;" json:"id"` |
| 20 | + UserID uuid.UUID `gorm:"column:userId;type:varchar;size:255;" json:"userId"` |
| 21 | + Hash string `gorm:"column:hash;type:varchar;size:255;" json:"hash"` |
| 22 | + Name string `gorm:"column:name;type:varchar;size:255;" json:"name"` |
| 23 | + Description string `gorm:"column:description;type:varchar;size:255;" json:"description"` |
| 24 | + Scopes []string `gorm:"column:scopes;type:text;size:65535;" json:"scopes"` |
| 25 | + ExpirationTime time.Time `gorm:"column:expirationTime;type:timestamp;" json:"expirationTime"` |
| 26 | + CreatedAt time.Time `gorm:"column:createdAt;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"createdAt"` |
| 27 | + LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"` |
| 28 | + |
| 29 | + // deleted is reserved for use by db-sync. |
| 30 | + _ bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"` |
| 31 | +} |
| 32 | + |
| 33 | +// TableName sets the insert table name for this struct type |
| 34 | +func (d *PersonalAccessToken) TableName() string { |
| 35 | + return "d_b_personal_access_token" |
| 36 | +} |
| 37 | + |
| 38 | +func GetToken(ctx context.Context, conn *gorm.DB, id string) (PersonalAccessToken, error) { |
| 39 | + var token PersonalAccessToken |
| 40 | + |
| 41 | + db := conn.WithContext(ctx) |
| 42 | + |
| 43 | + db = db.Where("id = ?", id).Find(&token) |
| 44 | + if db.Error != nil { |
| 45 | + return PersonalAccessToken{}, fmt.Errorf("Failed to retrieve token: %w", db.Error) |
| 46 | + } |
| 47 | + |
| 48 | + return token, nil |
| 49 | +} |
| 50 | + |
| 51 | +func CreateToken(ctx context.Context, conn *gorm.DB, req PersonalAccessToken) (PersonalAccessToken, error) { |
| 52 | + var token PersonalAccessToken |
| 53 | + |
| 54 | + if req.UserID == uuid.Nil { |
| 55 | + return PersonalAccessToken{}, status.Error(codes.InvalidArgument, "Invalid or empty userID") |
| 56 | + } |
| 57 | + if req.Hash == "" { |
| 58 | + return PersonalAccessToken{}, status.Error(codes.InvalidArgument, "Token hash required") |
| 59 | + } |
| 60 | + if req.Name == "" { |
| 61 | + return PersonalAccessToken{}, status.Error(codes.InvalidArgument, "Token name required") |
| 62 | + } |
| 63 | + if req.ExpirationTime.IsZero() { |
| 64 | + return PersonalAccessToken{}, status.Error(codes.InvalidArgument, "Expiration time required") |
| 65 | + } |
| 66 | + |
| 67 | + req.CreatedAt = time.Now().UTC() |
| 68 | + |
| 69 | + db := conn.WithContext(ctx).Create(req) |
| 70 | + if db.Error != nil { |
| 71 | + return PersonalAccessToken{}, fmt.Errorf("Failed to create token for user %s", req.UserID) |
| 72 | + } |
| 73 | + |
| 74 | + return token, nil |
| 75 | +} |
0 commit comments