MCPcopy Create free account
hub / github.com/gogs/gogs / Create

Method Create

internal/database/access_tokens.go:75–99  ·  view source on GitHub ↗

Create creates a new access token and persist to database. It returns ErrAccessTokenAlreadyExist when an access token with same name already exists for the user.

(ctx context.Context, userID int64, name string)

Source from the content-addressed store, hash-verified

73// ErrAccessTokenAlreadyExist when an access token with same name already exists
74// for the user.
75func (s *AccessTokensStore) Create(ctx context.Context, userID int64, name string) (*AccessToken, error) {
76 err := s.db.WithContext(ctx).Where("uid = ? AND name = ?", userID, name).First(new(AccessToken)).Error
77 if err == nil {
78 return nil, ErrAccessTokenAlreadyExist{args: errutil.Args{"userID": userID, "name": name}}
79 } else if !errors.Is(err, gorm.ErrRecordNotFound) {
80 return nil, err
81 }
82
83 token := cryptoutil.SHA1(gouuid.NewV4().String())
84 sha256 := cryptoutil.SHA256(token)
85
86 accessToken := &AccessToken{
87 UserID: userID,
88 Name: name,
89 Sha1: sha256[:40], // To pass the column unique constraint, keep the length of SHA1.
90 SHA256: sha256,
91 }
92 if err = s.db.WithContext(ctx).Create(accessToken).Error; err != nil {
93 return nil, err
94 }
95
96 // Set back the raw access token value, for the sake of the caller.
97 accessToken.Sha1 = token
98 return accessToken, nil
99}
100
101// DeleteByID deletes the access token by given ID.
102//

Callers

nothing calls this directly

Calls 4

SHA1Function · 0.92
SHA256Function · 0.92
WhereMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected