Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Refactoring access token from sha1 to sha1+JWT #115

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion gitea/user_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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.
Expand All @@ -36,10 +37,29 @@ func (c *Client) ListAccessTokens(user, pass string) ([]*AccessToken, error) {
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
Expand All @@ -52,7 +72,9 @@ func (c *Client) CreateAccessToken(user, pass string, opt CreateAccessTokenOptio
return t, c.getParsedResponse("POST", fmt.Sprintf("/users/%s/tokens", user),
http.Header{
"content-type": []string{"application/json"},
"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)}},
"Authorization": []string{"Basic " + BasicAuthEncode(user, pass)},
"X-Gitea-Server-Access-Token": []string{opt.GiteaServerAccessToken},
},
bytes.NewReader(body), t)
}

Expand Down