56
56
{"team" , "team:write" },
57
57
{"webhook" },
58
58
}
59
+
60
+ scopesApiMap = map [string ]string {
61
+ "account:read account:write" : "account:read" ,
62
+ "repository:admin repository:write" : "repository:write" ,
63
+ "repository:admin" : "repository:admin" ,
64
+ "team team:write" : "workspace membership:write (team:write)" ,
65
+ "webhook" : "webhook:read and write" ,
66
+ }
59
67
)
60
68
61
69
func NewBitbucketProvider (baseURL string , client * http.Client ) (Provider , error ) {
@@ -107,6 +115,7 @@ func (bb *bitbucket) verifyToken(ctx context.Context, token string, username str
107
115
if err != nil {
108
116
return fmt .Errorf ("failed checking token scope permission: %w" , err )
109
117
}
118
+
110
119
for _ , requiredScope := range requiredScopes {
111
120
isScopeIncluded := false
112
121
for _ , scopeOpt := range requiredScope {
@@ -115,7 +124,8 @@ func (bb *bitbucket) verifyToken(ctx context.Context, token string, username str
115
124
}
116
125
}
117
126
if ! isScopeIncluded {
118
- return fmt .Errorf ("the provided token is missing required token scopes, got: %s required: %v" , scopes , requiredScopes )
127
+ var requestedScopes = bb .getRequestedScopes (requiredScopes )
128
+ return fmt .Errorf ("the provided token is missing required token scopes, got: %s \n required: %v" , scopes , requestedScopes )
119
129
}
120
130
}
121
131
@@ -154,3 +164,29 @@ func (bb *bitbucket) request(ctx context.Context, username, token, method, urlPa
154
164
155
165
return bb .c .Do (req )
156
166
}
167
+
168
+ func (bb * bitbucket ) getRequestedScopes (requiredScopes [][]string ) string {
169
+ var requestedScopes string = ""
170
+
171
+ for _ , requiredScopeOpts := range requiredScopes {
172
+ var scopeOpts = ""
173
+ for _ , requiredScope := range requiredScopeOpts {
174
+ if len (scopeOpts ) > 0 {
175
+ scopeOpts += " "
176
+ }
177
+ scopeOpts += requiredScope
178
+ }
179
+
180
+ if len (requestedScopes ) > 0 {
181
+ requestedScopes += ", "
182
+ }
183
+
184
+ if len (scopesApiMap [scopeOpts ]) > 0 {
185
+ requestedScopes += scopesApiMap [scopeOpts ]
186
+ } else {
187
+ requestedScopes += scopeOpts
188
+ }
189
+ }
190
+
191
+ return requestedScopes
192
+ }
0 commit comments