2014-11-18 21:37:16 +05:30
|
|
|
// 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.
|
|
|
|
|
2015-12-05 03:46:42 +05:30
|
|
|
package user
|
2014-11-18 21:37:16 +05:30
|
|
|
|
|
|
|
import (
|
2016-11-11 15:09:44 +05:30
|
|
|
api "code.gitea.io/sdk/gitea"
|
2014-11-18 21:37:16 +05:30
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2014-11-18 21:37:16 +05:30
|
|
|
)
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// ListAccessTokens list all the access tokens
|
2016-03-14 04:19:16 +05:30
|
|
|
func ListAccessTokens(ctx *context.APIContext) {
|
2017-05-02 19:05:59 +05:30
|
|
|
// swagger:route GET /users/{username}/tokens userGetTokens
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// - application/json
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: AccessTokenList
|
|
|
|
// 500: error
|
|
|
|
|
2016-07-23 22:38:22 +05:30
|
|
|
tokens, err := models.ListAccessTokens(ctx.User.ID)
|
2014-11-18 21:37:16 +05:30
|
|
|
if err != nil {
|
2016-03-14 04:19:16 +05:30
|
|
|
ctx.Error(500, "ListAccessTokens", err)
|
2014-11-18 21:37:16 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
apiTokens := make([]*api.AccessToken, len(tokens))
|
|
|
|
for i := range tokens {
|
2017-02-26 10:55:35 +05:30
|
|
|
apiTokens[i] = &api.AccessToken{
|
|
|
|
Name: tokens[i].Name,
|
|
|
|
Sha1: tokens[i].Sha1,
|
|
|
|
}
|
2014-11-18 21:37:16 +05:30
|
|
|
}
|
|
|
|
ctx.JSON(200, &apiTokens)
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// CreateAccessToken create access tokens
|
2016-03-14 04:19:16 +05:30
|
|
|
func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption) {
|
2017-05-02 19:05:59 +05:30
|
|
|
// swagger:route POST /users/{username} /tokens userCreateToken
|
|
|
|
//
|
|
|
|
// Consumes:
|
|
|
|
// - application/json
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
// - application/json
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: AccessToken
|
|
|
|
// 500: error
|
|
|
|
|
2014-11-18 21:37:16 +05:30
|
|
|
t := &models.AccessToken{
|
2016-07-23 22:38:22 +05:30
|
|
|
UID: ctx.User.ID,
|
2014-11-18 21:37:16 +05:30
|
|
|
Name: form.Name,
|
|
|
|
}
|
|
|
|
if err := models.NewAccessToken(t); err != nil {
|
2016-03-14 04:19:16 +05:30
|
|
|
ctx.Error(500, "NewAccessToken", err)
|
2014-11-18 21:37:16 +05:30
|
|
|
return
|
|
|
|
}
|
2017-02-26 10:55:35 +05:30
|
|
|
ctx.JSON(201, &api.AccessToken{
|
|
|
|
Name: t.Name,
|
|
|
|
Sha1: t.Sha1,
|
|
|
|
})
|
2014-11-18 21:37:16 +05:30
|
|
|
}
|