8 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/requests"
14// BitbucketProvider represents an Bitbucket based Identity Provider
15type BitbucketProvider struct {
21var _ Provider = (*BitbucketProvider)(nil)
24 bitbucketProviderName = "Bitbucket"
25 bitbucketDefaultScope = "email"
29 // Default Login URL for Bitbucket.
30 // Pre-parsed URL of https://bitbucket.org/site/oauth2/authorize.
31 bitbucketDefaultLoginURL = &url.URL{
33 Host: "bitbucket.org",
34 Path: "/site/oauth2/authorize",
37 // Default Redeem URL for Bitbucket.
38 // Pre-parsed URL of https://bitbucket.org/site/oauth2/access_token.
39 bitbucketDefaultRedeemURL = &url.URL{
41 Host: "bitbucket.org",
42 Path: "/site/oauth2/access_token",
45 // Default Validation URL for Bitbucket.
46 // This simply returns the email of the authenticated user.
47 // Bitbucket does not have a Profile URL to use.
48 // Pre-parsed URL of https://api.bitbucket.org/2.0/user/emails.
49 bitbucketDefaultValidateURL = &url.URL{
51 Host: "api.bitbucket.org",
52 Path: "/2.0/user/emails",
56// NewBitbucketProvider initiates a new BitbucketProvider
57func NewBitbucketProvider(p *ProviderData, opts options.BitbucketOptions) *BitbucketProvider {
58 p.setProviderDefaults(providerDefaults{
59 name: bitbucketProviderName,
60 loginURL: bitbucketDefaultLoginURL,
61 redeemURL: bitbucketDefaultRedeemURL,
63 validateURL: bitbucketDefaultValidateURL,
64 scope: bitbucketDefaultScope,
67 provider := &BitbucketProvider{ProviderData: p}
70 provider.setTeam(opts.Team)
72 if opts.Repository != "" {
73 provider.setRepository(opts.Repository)
78// setTeam defines the Bitbucket team the user must be part of
79func (p *BitbucketProvider) setTeam(team string) {
81 if !strings.Contains(p.Scope, "team") {
86// setRepository defines the repository the user must have access to
87func (p *BitbucketProvider) setRepository(repository string) {
88 p.Repository = repository
89 if !strings.Contains(p.Scope, "repository") {
90 p.Scope += " repository"
94// GetEmailAddress returns the email of the authenticated user
95func (p *BitbucketProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
99 Email string `json:"email"`
100 Primary bool `json:"is_primary"`
105 Name string `json:"username"`
108 var repositories struct {
110 FullName string `json:"full_name"`
114 requestURL := p.ValidateURL.String() + "?access_token=" + s.AccessToken
115 err := requests.New(requestURL).
118 UnmarshalInto(&emails)
120 logger.Errorf("failed making request: %v", err)
125 teamURL := &url.URL{}
126 *teamURL = *p.ValidateURL
127 teamURL.Path = "/2.0/teams"
129 requestURL := teamURL.String() + "?role=member&access_token=" + s.AccessToken
131 err := requests.New(requestURL).
134 UnmarshalInto(&teams)
136 logger.Errorf("failed requesting teams membership: %v", err)
140 for _, team := range teams.Values {
141 if p.Team == team.Name {
147 logger.Error("team membership test failed, access denied")
152 if p.Repository != "" {
153 repositoriesURL := &url.URL{}
154 *repositoriesURL = *p.ValidateURL
155 repositoriesURL.Path = "/2.0/repositories/" + strings.Split(p.Repository, "/")[0]
157 requestURL := repositoriesURL.String() + "?role=contributor" +
158 "&q=full_name=" + url.QueryEscape("\""+p.Repository+"\"") +
159 "&access_token=" + s.AccessToken
161 err := requests.New(requestURL).
164 UnmarshalInto(&repositories)
166 logger.Errorf("failed checking repository access: %v", err)
171 for _, repository := range repositories.Values {
172 if p.Repository == repository.FullName {
178 logger.Error("repository access test failed, access denied")
183 for _, email := range emails.Values {
185 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