@@ -92,7 +92,7 @@ func OauthRedirectHandler(app *server.App) http.HandlerFunc {
9292 return
9393 }
9494
95- user , err := oAuthCodeToUser (r .Context (), app .GitHubOAuthConfig , query .Code )
95+ githubData , err := oAuthCodeToUser (r .Context (), app .GitHubOAuthConfig , query .Code )
9696 if err != nil {
9797 app .Logger .Error ().
9898 Caller ().
@@ -107,8 +107,17 @@ func OauthRedirectHandler(app *server.App) http.HandlerFunc {
107107 return
108108 }
109109
110- authRequest .GitHubUserID = cast .ToString (user .GetID ())
111- authRequest .GitHubUserLogin = user .GetLogin ()
110+ authRequest .GitHubUserID = cast .ToString (githubData .User .GetID ())
111+ authRequest .GitHubUserLogin = githubData .User .GetLogin ()
112+
113+ if authRequest .GithubTeamIDs != nil {
114+ var teamIDs []string
115+ for _ , team := range githubData .Teams {
116+ teamIDs = append (teamIDs , cast .ToString (team .GetID ()))
117+ }
118+
119+ authRequest .GithubTeamIDs = teamIDs
120+ }
112121
113122 authURL , err := url .Parse (authRequest .AuthURL )
114123 if err != nil {
@@ -163,12 +172,18 @@ func OauthAuthResultHandler(app *server.App) http.HandlerFunc {
163172 RedirectURI : authRequest .RedirectURI ,
164173 GitHubUserID : authRequest .GitHubUserID ,
165174 GitHubUserLogin : authRequest .GitHubUserLogin ,
175+ GithubTeamIDs : authRequest .GithubTeamIDs ,
166176 },
167177 )
168178 }
169179}
170180
171- func oAuthCodeToUser (ctx context.Context , oAuthConfig * oauth2.Config , code string ) (* github.User , error ) {
181+ type oauthCodeToUserResponse struct {
182+ User * github.User
183+ Teams []* github.Team
184+ }
185+
186+ func oAuthCodeToUser (ctx context.Context , oAuthConfig * oauth2.Config , code string ) (* oauthCodeToUserResponse , error ) {
172187 ctxExchange , cancelExchange := context .WithCancel (ctx )
173188 defer cancelExchange ()
174189 token , err := oAuthConfig .Exchange (ctxExchange , code )
@@ -181,14 +196,29 @@ func oAuthCodeToUser(ctx context.Context, oAuthConfig *oauth2.Config, code strin
181196 gitHubApiHttpClient := oAuthConfig .Client (ctxClient , token )
182197 gitHubApiClient := github .NewClient (gitHubApiHttpClient )
183198
199+ // Get user information, login and ID
184200 ctxGetUser , cancelGetUser := context .WithCancel (ctx )
185201 defer cancelGetUser ()
186-
187202 user , _ , err := gitHubApiClient .Users .Get (ctxGetUser , "" )
188203 if err != nil {
189204 return nil , err
190205 }
191- return user , nil
206+
207+ // Optionally, check if the user is a member of any teams and retrieve them
208+ // This won't cancel the main request
209+ ctxTeams , cancelListTeams := context .WithCancel (ctx )
210+ teams , _ , err := gitHubApiClient .Teams .ListUserTeams (ctxTeams , & github.ListOptions {PerPage : 100 })
211+ defer cancelListTeams ()
212+ if err != nil {
213+ // If the user is not a member of any teams, the API will return a 404
214+ // We can ignore this error since this is not a mandatory request
215+ teams = nil
216+ }
217+
218+ return & oauthCodeToUserResponse {
219+ User : user ,
220+ Teams : teams ,
221+ }, nil
192222}
193223
194224func buildRedirectURI (apiBaseUrl , rid string ) (string , error ) {
0 commit comments