Skip to content

Commit ca851a2

Browse files
author
Laurie T. Malau
committed
[public api] personal access token db model
1 parent ca3f94d commit ca851a2

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

components/common-go/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636

3737
require (
3838
github.com/go-sql-driver/mysql v1.6.0
39+
github.com/google/uuid v1.1.2
3940
github.com/relvacode/iso8601 v1.1.0
4041
gorm.io/driver/mysql v1.4.4
4142
gorm.io/gorm v1.24.1

components/common-go/go.sum

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)