mirror of
https://github.com/gogs/gogs.git
synced 2025-05-21 06:50:40 +00:00
* Bootstrap with GORM * Fix lint error * Set conn max lifetime to one minute * Fallback to use gorm v1 * Define HTTP routes * Finish authentication * Save token updated * Add docstring * Finish authorization * serveBatch rundown * Define types in lfsutil * Finish Batch * authutil * Finish basic * Formalize response error * Fix lint errors * authutil: add tests * dbutil: add tests * lfsutil: add tests * strutil: add tests * Formalize 401 response
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package user
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
api "github.com/gogs/go-gogs-client"
|
|
|
|
"gogs.io/gogs/internal/context"
|
|
"gogs.io/gogs/internal/db"
|
|
"gogs.io/gogs/internal/db/errors"
|
|
)
|
|
|
|
func ListAccessTokens(c *context.APIContext) {
|
|
tokens, err := db.ListAccessTokens(c.User.ID)
|
|
if err != nil {
|
|
c.Error(err, "list access tokens")
|
|
return
|
|
}
|
|
|
|
apiTokens := make([]*api.AccessToken, len(tokens))
|
|
for i := range tokens {
|
|
apiTokens[i] = &api.AccessToken{Name: tokens[i].Name, Sha1: tokens[i].Sha1}
|
|
}
|
|
c.JSONSuccess(&apiTokens)
|
|
}
|
|
|
|
func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) {
|
|
t := &db.AccessToken{
|
|
UserID: c.User.ID,
|
|
Name: form.Name,
|
|
}
|
|
if err := db.NewAccessToken(t); err != nil {
|
|
if errors.IsAccessTokenNameAlreadyExist(err) {
|
|
c.ErrorStatus(http.StatusUnprocessableEntity, err)
|
|
} else {
|
|
c.Error(err, "new access token")
|
|
}
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, &api.AccessToken{Name: t.Name, Sha1: t.Sha1})
|
|
}
|