Debian Codegraph @

internal/oauth2/providers/github/user.go

raw
1 package github
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "log/slog"
8 "strconv"
9
10 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken"
11 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types"
12 )
13
14 // user holds GitHub user information as defined by
15 // https://developer.github.com/v3/users/#response-with-public-profile-information
16 type userType struct {
17 Name string `json:"name"`
18 Login string `json:"login"`
19 Email string `json:"email"`
20 ID int `json:"id"`
21 }
22
23 // GetUser fetches the authenticated GitHub user through the REST API.
24 func (p Provider) GetUser(ctx context.Context, _ *slog.Logger, tokens *idtoken.IDToken, _ *types.UserInfo) (types.UserInfo, error) {
25 if tokens.AccessToken == "" {
26 return types.UserInfo{}, errors.New("access token is empty")
27 }
28
29 var user userType
30
31 _, err := get[userType](ctx, p.httpClient, tokens.AccessToken, "https://api.github.com/user", &user)
32 if err != nil {
33 return types.UserInfo{}, err
34 }
35
36 userInfo := types.UserInfo{
37 Username: user.Login,
38 Email: user.Email,
39 Subject: strconv.Itoa(user.ID),
40 }
41
42 organizations, err := p.getOrganizations(ctx, tokens)
43 if err != nil {
44 return types.UserInfo{}, fmt.Errorf("error getting GitHub organizations: %w", err)
45 }
46
47 userInfo.Groups = organizations
48
49 teams, err := p.getTeams(ctx, tokens)
50 if err != nil {
51 return types.UserInfo{}, fmt.Errorf("error getting GitHub teams: %w", err)
52 }
53
54 userInfo.Roles = teams
55
56 return userInfo, nil
57 }