Files
Gogs/internal/route/api/v1/user/app.go

42 lines
1.1 KiB
Go
Raw Normal View History

2014-11-18 11:07:16 -05:00
// 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
2014-11-18 11:07:16 -05:00
import (
2019-08-08 23:53:43 -07:00
"net/http"
2018-05-27 08:53:48 +08:00
api "github.com/gogs/go-gogs-client"
2014-11-18 11:07:16 -05:00
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
2014-11-18 11:07:16 -05:00
)
2017-06-03 07:26:09 -04:00
func ListAccessTokens(c *context.APIContext) {
tokens, err := db.AccessTokens.List(c.Req.Context(), c.User.ID)
2014-11-18 11:07:16 -05:00
if err != nil {
c.Error(err, "list access tokens")
2014-11-18 11:07:16 -05:00
return
}
apiTokens := make([]*api.AccessToken, len(tokens))
for i := range tokens {
apiTokens[i] = &api.AccessToken{Name: tokens[i].Name, Sha1: tokens[i].Sha1}
2014-11-18 11:07:16 -05:00
}
2019-08-08 23:53:43 -07:00
c.JSONSuccess(&apiTokens)
2014-11-18 11:07:16 -05:00
}
2017-06-03 07:26:09 -04:00
func CreateAccessToken(c *context.APIContext, form api.CreateAccessTokenOption) {
t, err := db.AccessTokens.Create(c.Req.Context(), c.User.ID, form.Name)
if err != nil {
if db.IsErrAccessTokenAlreadyExist(err) {
c.ErrorStatus(http.StatusUnprocessableEntity, err)
} else {
c.Error(err, "new access token")
}
2014-11-18 11:07:16 -05:00
return
}
c.JSON(http.StatusCreated, &api.AccessToken{Name: t.Name, Sha1: t.Sha1})
2014-11-18 11:07:16 -05:00
}