GetUserInfo github user info, calls github api for org and teams
(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens, opts ...oauth2.AuthCodeOption)
| 38 | |
| 39 | // GetUserInfo github user info, calls github api for org and teams |
| 40 | func (me Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens, opts ...oauth2.AuthCodeOption) (rerr error) { |
| 41 | client, _, err := me.PrepareTokensAndClient(r, ptokens, true, opts...) |
| 42 | if err != nil { |
| 43 | return err |
| 44 | } |
| 45 | userinfo, err := client.Get(cfg.GenOAuth.UserInfoURL) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | defer func() { |
| 50 | if err := userinfo.Body.Close(); err != nil { |
| 51 | rerr = err |
| 52 | } |
| 53 | }() |
| 54 | data, _ := io.ReadAll(userinfo.Body) |
| 55 | log.Infof("github userinfo body: %s", string(data)) |
| 56 | if err = common.MapClaims(data, customClaims); err != nil { |
| 57 | log.Error(err) |
| 58 | return err |
| 59 | } |
| 60 | ghUser := structs.GitHubUser{} |
| 61 | if err = json.Unmarshal(data, &ghUser); err != nil { |
| 62 | log.Error(err) |
| 63 | return err |
| 64 | } |
| 65 | log.Debug("getUserInfoFromGitHub ghUser") |
| 66 | log.Debug(ghUser) |
| 67 | log.Debug("getUserInfoFromGitHub user") |
| 68 | log.Debug(user) |
| 69 | |
| 70 | ghUser.PrepareUserData() |
| 71 | user.Email = ghUser.Email |
| 72 | user.Name = ghUser.Name |
| 73 | user.Username = ghUser.Username |
| 74 | user.ID = ghUser.ID |
| 75 | |
| 76 | // user = &ghUser.User |
| 77 | |
| 78 | toOrgAndTeam := func(orgAndTeam string) (string, string) { |
| 79 | split := strings.Split(orgAndTeam, "/") |
| 80 | if len(split) == 1 { |
| 81 | // only organization given |
| 82 | return orgAndTeam, "" |
| 83 | } else if len(split) == 2 { |
| 84 | return split[0], split[1] |
| 85 | } else { |
| 86 | return "", "" |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if len(cfg.Cfg.TeamWhiteList) != 0 { |
| 91 | for _, orgAndTeam := range cfg.Cfg.TeamWhiteList { |
| 92 | org, team := toOrgAndTeam(orgAndTeam) |
| 93 | if org != "" { |
| 94 | log.Info(org) |
| 95 | var err error |
| 96 | isMember := false |
| 97 | if team != "" { |