Skip to content

Commit 2cdf568

Browse files
add Github provider for oauth2
1 parent e94e986 commit 2cdf568

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

oauth2/providers.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
const (
2121
googleInfoEndpoint = `https://www.googleapis.com/userinfo/v2/me`
2222
facebookInfoEndpoint = `https://graph.facebook.com/me?fields=name,email`
23+
githubInfoEndpoint = `https://api.github.com/user`
2324
)
2425

2526
type googleMeResponse struct {
@@ -88,3 +89,37 @@ func FacebookUserDetails(ctx context.Context, cfg oauth2.Config, token *oauth2.T
8889
OAuth2Name: response.Name,
8990
}, nil
9091
}
92+
93+
type githubMeResponse struct {
94+
ID string `json:"id"`
95+
Email string `json:"email"`
96+
Name string `json:"name"`
97+
}
98+
99+
// GithubUserDetails can be used as a FindUserDetails function
100+
// for an authboss.OAuth2Provider
101+
func GithubUserDetails(ctx context.Context, cfg oauth2.Config, token *oauth2.Token) (map[string]string, error) {
102+
client := cfg.Client(ctx, token)
103+
resp, err := clientGet(client, githubInfoEndpoint)
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
defer resp.Body.Close()
109+
byt, err := io.ReadAll(resp.Body)
110+
if err != nil {
111+
return nil, errors.Wrap(err, "failed to read body from github oauth2 endpoint")
112+
}
113+
114+
var response githubMeResponse
115+
if err = json.Unmarshal(byt, &response); err != nil {
116+
return nil, errors.Wrap(err, "failed to parse json from github oauth2 endpoint")
117+
}
118+
119+
return map[string]string{
120+
OAuth2UID: response.ID,
121+
OAuth2Email: response.Email,
122+
OAuth2Name: response.Name,
123+
}, nil
124+
125+
}

0 commit comments

Comments
 (0)