This repository was archived by the owner on Jun 8, 2019. It is now read-only.
forked from gogs/go-gogs-client
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathuser_app.go
85 lines (75 loc) · 3.2 KB
/
user_app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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 gitea
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
)
// BasicAuthEncode generate base64 of basic auth head
func BasicAuthEncode(user, pass string) string {
return base64.StdEncoding.EncodeToString([]byte(user + ":" + pass))
}
// AccessToken represents a API access token.
// swagger:response AccessToken
type AccessToken struct {
ID int64 `json:"id"`
Name string `json:"name"`
Sha1 string `json:"sha1"`
Content string `json:"content,omitempty"`
}
// AccessTokenList represents a list of API access token.
// swagger:response AccessTokenList
type AccessTokenList []*AccessToken
// ListAccessTokens lista all the access tokens of user
func (c *Client) ListAccessTokens(user, pass string) ([]*AccessToken, error) {
tokens := make([]*AccessToken, 0, 10)
return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens", user),
http.Header{"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}}, nil, &tokens)
}
// AdminListAccessTokens lista all the access tokens of user, authenticate with pre-generated server token.
// this allows server app to list and generate access token for a user already authenticated through other means.
func (c *Client) AdminListAccessTokens(user, serverToken string) ([]*AccessToken, error) {
tokens := make([]*AccessToken, 0, 10)
return tokens, c.getParsedResponse("GET", fmt.Sprintf("/users/%s/tokens", user),
http.Header{"X-Gitea-Server-Access-Token": []string{serverToken} }, nil, &tokens)
}
// CreateAccessTokenOption options when create access token
// swagger:parameters userCreateToken
type CreateAccessTokenOption struct {
Name string `json:"name" binding:"Required"`
MatchOwner []string `json:"match_owner,omitempty"`
MatchRepo []string `json:"match_repo,omitempty"`
WildcardMatchBranch []string `json:"wildcard_match_branch,omitempty"`
WildcardMatchRoute []string `json:"wildcard_match_route,omitempty"`
MatchMethod []string `json:"match_method,omitempty"`
ExpiresAt int64 `json:"expires_at,omitempty"`
// allow integrated server app to authenticate by pre-generated token
// and to deprecate basic auth by username and password.
// this will also give server app the option to generate user access token
// on the fly without storing token.
GiteaServerAccessToken string `json:"-"`
}
// CreateAccessToken create one access token with options
func (c *Client) CreateAccessToken(user, pass string, opt CreateAccessTokenOption) (*AccessToken, error) {
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"},
"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)},
"X-Gitea-Server-Access-Token": []string{opt.GiteaServerAccessToken},
},
bytes.NewReader(body), t)
}
// DeleteAccessToken delete token with key id
func (c *Client) DeleteAccessToken(user string, keyID int64) error {
_, err := c.getResponse("DELETE", fmt.Sprintf("/user/%s/tokens/%d", user, keyID), nil, nil)
return err
}