2014-11-18 16:06:47 +00:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2019-04-19 07:53:46 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2014-11-18 16:06:47 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-08-26 14:34:27 +01:00
|
|
|
package gitea
|
2014-11-18 16:06:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2020-02-02 07:05:40 +00:00
|
|
|
// basicAuthEncode generate base64 of basic auth head
|
|
|
|
func basicAuthEncode(user, pass string) string {
|
2014-11-18 16:06:47 +00:00
|
|
|
return base64.StdEncoding.EncodeToString([]byte(user + ":" + pass))
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// AccessToken represents an API access token.
|
|
|
|
type AccessToken struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Token string `json:"sha1"`
|
|
|
|
TokenLastEight string `json:"token_last_eight"`
|
|
|
|
}
|
2017-04-30 16:33:16 +01:00
|
|
|
|
2020-02-05 07:59:55 +00:00
|
|
|
// ListAccessTokensOptions options for listing a users's access tokens
|
|
|
|
type ListAccessTokensOptions struct {
|
|
|
|
ListOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListAccessTokens lists all the access tokens of user
|
|
|
|
func (c *Client) ListAccessTokens(user, pass string, opts ListAccessTokensOptions) ([]*AccessToken, error) {
|
|
|
|
opts.setDefaults()
|
|
|
|
tokens := make([]*AccessToken, 0, opts.PageSize)
|
|
|
|
return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens?%s", user, opts.getURLQuery().Encode()),
|
2020-02-02 07:05:40 +00:00
|
|
|
http.Header{"Authorization": []string{"Basic " + basicAuthEncode(user, pass)}}, nil, &tokens)
|
2014-11-18 16:06:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// CreateAccessTokenOption options when create access token
|
|
|
|
type CreateAccessTokenOption struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// CreateAccessToken create one access token with options
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) CreateAccessToken(user, pass string, opt CreateAccessTokenOption) (*AccessToken, error) {
|
2014-11-18 16:06:47 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t := new(AccessToken)
|
|
|
|
return t, c.getParsedResponse("POST", fmt.Sprintf("/users/%s/tokens", user),
|
|
|
|
http.Header{
|
|
|
|
"content-type": []string{"application/json"},
|
2020-02-02 07:05:40 +00:00
|
|
|
"Authorization": []string{"Basic " + basicAuthEncode(user, pass)}},
|
2014-11-18 16:06:47 +00:00
|
|
|
bytes.NewReader(body), t)
|
|
|
|
}
|
2018-06-20 10:04:31 +01:00
|
|
|
|
|
|
|
// DeleteAccessToken delete token with key id
|
2020-02-02 07:05:40 +00:00
|
|
|
func (c *Client) DeleteAccessToken(user, pass string, keyID int64) error {
|
|
|
|
_, err := c.getResponse("DELETE", fmt.Sprintf("/users/%s/tokens/%d", user, keyID),
|
|
|
|
http.Header{"Authorization": []string{"Basic " + basicAuthEncode(user, pass)}}, nil)
|
2018-06-20 10:04:31 +01:00
|
|
|
return err
|
|
|
|
}
|