| 1 |
package github |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
|
| 8 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken" |
| 9 |
) |
| 10 |
|
| 11 |
type orgType struct { |
| 12 |
Login string `json:"login"` |
| 13 |
} |
| 14 |
|
| 15 |
type teamType struct { |
| 16 |
Org orgType `json:"organization"` |
| 17 |
Slug string `json:"slug"` |
| 18 |
} |
| 19 |
|
| 20 |
// getTeams fetch the users GitHub teams by accessing the GitHub API. |
| 21 |
func (p Provider) getTeams(ctx context.Context, tokens *idtoken.IDToken) ([]string, error) { |
| 22 |
if tokens.AccessToken == "" { |
| 23 |
return nil, errors.New("access token is empty") |
| 24 |
} |
| 25 |
|
| 26 |
var roles []string |
| 27 |
|
| 28 |
apiURL := "https://api.github.com/user/teams" |
| 29 |
|
| 30 |
for { |
| 31 |
var ( |
| 32 |
teams []teamType |
| 33 |
err error |
| 34 |
) |
| 35 |
if apiURL, err = get[[]teamType](ctx, p.httpClient, tokens.AccessToken, apiURL, &teams); err != nil { |
| 36 |
return nil, err |
| 37 |
} |
| 38 |
|
| 39 |
for _, team := range teams { |
| 40 |
roles = append(roles, fmt.Sprintf("%s:%s", team.Org.Login, team.Slug)) |
| 41 |
} |
| 42 |
|
| 43 |
if apiURL == "" { |
| 44 |
break |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return roles, nil |
| 49 |
} |
| 50 |
|
| 51 |
// getOrganizations fetch the users GitHub organization by accessing the GitHub API. |
| 52 |
func (p Provider) getOrganizations(ctx context.Context, tokens *idtoken.IDToken) ([]string, error) { |
| 53 |
if tokens.AccessToken == "" { |
| 54 |
return nil, errors.New("access token is empty") |
| 55 |
} |
| 56 |
|
| 57 |
var groups []string |
| 58 |
|
| 59 |
apiURL := "https://api.github.com/user/orgs" |
| 60 |
|
| 61 |
for { |
| 62 |
var ( |
| 63 |
orgs []orgType |
| 64 |
err error |
| 65 |
) |
| 66 |
if apiURL, err = get[[]orgType](ctx, p.httpClient, tokens.AccessToken, apiURL, &orgs); err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
|
| 70 |
for _, org := range orgs { |
| 71 |
groups = append(groups, org.Login) |
| 72 |
} |
| 73 |
|
| 74 |
if apiURL == "" { |
| 75 |
break |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return groups, nil |
| 80 |
} |