8 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
13// BitbucketProvider represents an Bitbucket based Identity Provider
14type BitbucketProvider struct {
20var _ Provider = (*BitbucketProvider)(nil)
23 bitbucketProviderName = "Bitbucket"
24 bitbucketDefaultScope = "email"
28 // Default Login URL for Bitbucket.
29 // Pre-parsed URL of https://bitbucket.org/site/oauth2/authorize.
30 bitbucketDefaultLoginURL = &url.URL{
32 Host: "bitbucket.org",
33 Path: "/site/oauth2/authorize",
36 // Default Redeem URL for Bitbucket.
37 // Pre-parsed URL of https://bitbucket.org/site/oauth2/access_token.
38 bitbucketDefaultRedeemURL = &url.URL{
40 Host: "bitbucket.org",
41 Path: "/site/oauth2/access_token",
44 // Default Validation URL for Bitbucket.
45 // This simply returns the email of the authenticated user.
46 // Bitbucket does not have a Profile URL to use.
47 // Pre-parsed URL of https://api.bitbucket.org/2.0/user/emails.
48 bitbucketDefaultValidateURL = &url.URL{
50 Host: "api.bitbucket.org",
51 Path: "/2.0/user/emails",
55// NewBitbucketProvider initiates a new BitbucketProvider
56func NewBitbucketProvider(p *ProviderData) *BitbucketProvider {
57 p.setProviderDefaults(providerDefaults{
58 name: bitbucketProviderName,
59 loginURL: bitbucketDefaultLoginURL,
60 redeemURL: bitbucketDefaultRedeemURL,
62 validateURL: bitbucketDefaultValidateURL,
63 scope: bitbucketDefaultScope,
65 return &BitbucketProvider{ProviderData: p}
68// SetTeam defines the Bitbucket team the user must be part of
69func (p *BitbucketProvider) SetTeam(team string) {
71 if !strings.Contains(p.Scope, "team") {
76// SetRepository defines the repository the user must have access to
77func (p *BitbucketProvider) SetRepository(repository string) {
78 p.Repository = repository
79 if !strings.Contains(p.Scope, "repository") {
80 p.Scope += " repository"
84// GetEmailAddress returns the email of the authenticated user
85func (p *BitbucketProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
89 Email string `json:"email"`
90 Primary bool `json:"is_primary"`
95 Name string `json:"username"`
98 var repositories struct {
100 FullName string `json:"full_name"`
104 requestURL := p.ValidateURL.String() + "?access_token=" + s.AccessToken
105 err := requests.New(requestURL).
108 UnmarshalInto(&emails)
110 logger.Errorf("failed making request: %v", err)
115 teamURL := &url.URL{}
116 *teamURL = *p.ValidateURL
117 teamURL.Path = "/2.0/teams"
119 requestURL := teamURL.String() + "?role=member&access_token=" + s.AccessToken
121 err := requests.New(requestURL).
124 UnmarshalInto(&teams)
126 logger.Errorf("failed requesting teams membership: %v", err)
130 for _, team := range teams.Values {
131 if p.Team == team.Name {
137 logger.Error("team membership test failed, access denied")
142 if p.Repository != "" {
143 repositoriesURL := &url.URL{}
144 *repositoriesURL = *p.ValidateURL
145 repositoriesURL.Path = "/2.0/repositories/" + strings.Split(p.Repository, "/")[0]
147 requestURL := repositoriesURL.String() + "?role=contributor" +
148 "&q=full_name=" + url.QueryEscape("\""+p.Repository+"\"") +
149 "&access_token=" + s.AccessToken
151 err := requests.New(requestURL).
154 UnmarshalInto(&repositories)
156 logger.Errorf("failed checking repository access: %v", err)
161 for _, repository := range repositories.Values {
162 if p.Repository == repository.FullName {
168 logger.Error("repository access test failed, access denied")
173 for _, email := range emails.Values {
175 return email.Email, nil
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list