mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
Improve providers.
This commit is contained in:
113
idp/github.go
113
idp/github.go
@ -20,6 +20,7 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
@ -100,12 +101,102 @@ func (idp *GithubIdProvider) getEmail(token *oauth2.Token) string {
|
||||
return res
|
||||
}
|
||||
|
||||
//{
|
||||
// "login": "jimgreen",
|
||||
// "id": 3781234,
|
||||
// "node_id": "MDQ6VXNlcjM3O123456=",
|
||||
// "avatar_url": "https://avatars.githubusercontent.com/u/3781234?v=4",
|
||||
// "gravatar_id": "",
|
||||
// "url": "https://api.github.com/users/jimgreen",
|
||||
// "html_url": "https://github.com/jimgreen",
|
||||
// "followers_url": "https://api.github.com/users/jimgreen/followers",
|
||||
// "following_url": "https://api.github.com/users/jimgreen/following{/other_user}",
|
||||
// "gists_url": "https://api.github.com/users/jimgreen/gists{/gist_id}",
|
||||
// "starred_url": "https://api.github.com/users/jimgreen/starred{/owner}{/repo}",
|
||||
// "subscriptions_url": "https://api.github.com/users/jimgreen/subscriptions",
|
||||
// "organizations_url": "https://api.github.com/users/jimgreen/orgs",
|
||||
// "repos_url": "https://api.github.com/users/jimgreen/repos",
|
||||
// "events_url": "https://api.github.com/users/jimgreen/events{/privacy}",
|
||||
// "received_events_url": "https://api.github.com/users/jimgreen/received_events",
|
||||
// "type": "User",
|
||||
// "site_admin": false,
|
||||
// "name": "Jim Green",
|
||||
// "company": "Casbin",
|
||||
// "blog": "https://casbin.org",
|
||||
// "location": "Bay Area",
|
||||
// "email": "jimgreen@gmail.com",
|
||||
// "hireable": true,
|
||||
// "bio": "My bio",
|
||||
// "twitter_username": null,
|
||||
// "public_repos": 45,
|
||||
// "public_gists": 3,
|
||||
// "followers": 123,
|
||||
// "following": 31,
|
||||
// "created_at": "2016-03-06T13:16:13Z",
|
||||
// "updated_at": "2020-05-30T12:15:29Z",
|
||||
// "private_gists": 0,
|
||||
// "total_private_repos": 12,
|
||||
// "owned_private_repos": 12,
|
||||
// "disk_usage": 46331,
|
||||
// "collaborators": 5,
|
||||
// "two_factor_authentication": true,
|
||||
// "plan": {
|
||||
// "name": "free",
|
||||
// "space": 976562499,
|
||||
// "collaborators": 0,
|
||||
// "private_repos": 10000
|
||||
// }
|
||||
//}
|
||||
|
||||
type GitHubUserInfo struct {
|
||||
Login string `json:"login"`
|
||||
Id int `json:"id"`
|
||||
NodeId string `json:"node_id"`
|
||||
AvatarUrl string `json:"avatar_url"`
|
||||
GravatarId string `json:"gravatar_id"`
|
||||
Url string `json:"url"`
|
||||
HtmlUrl string `json:"html_url"`
|
||||
FollowersUrl string `json:"followers_url"`
|
||||
FollowingUrl string `json:"following_url"`
|
||||
GistsUrl string `json:"gists_url"`
|
||||
StarredUrl string `json:"starred_url"`
|
||||
SubscriptionsUrl string `json:"subscriptions_url"`
|
||||
OrganizationsUrl string `json:"organizations_url"`
|
||||
ReposUrl string `json:"repos_url"`
|
||||
EventsUrl string `json:"events_url"`
|
||||
ReceivedEventsUrl string `json:"received_events_url"`
|
||||
Type string `json:"type"`
|
||||
SiteAdmin bool `json:"site_admin"`
|
||||
Name string `json:"name"`
|
||||
Company string `json:"company"`
|
||||
Blog string `json:"blog"`
|
||||
Location string `json:"location"`
|
||||
Email string `json:"email"`
|
||||
Hireable bool `json:"hireable"`
|
||||
Bio string `json:"bio"`
|
||||
TwitterUsername interface{} `json:"twitter_username"`
|
||||
PublicRepos int `json:"public_repos"`
|
||||
PublicGists int `json:"public_gists"`
|
||||
Followers int `json:"followers"`
|
||||
Following int `json:"following"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
PrivateGists int `json:"private_gists"`
|
||||
TotalPrivateRepos int `json:"total_private_repos"`
|
||||
OwnedPrivateRepos int `json:"owned_private_repos"`
|
||||
DiskUsage int `json:"disk_usage"`
|
||||
Collaborators int `json:"collaborators"`
|
||||
TwoFactorAuthentication bool `json:"two_factor_authentication"`
|
||||
Plan struct {
|
||||
Name string `json:"name"`
|
||||
Space int `json:"space"`
|
||||
Collaborators int `json:"collaborators"`
|
||||
PrivateRepos int `json:"private_repos"`
|
||||
} `json:"plan"`
|
||||
}
|
||||
|
||||
func (idp *GithubIdProvider) getLoginAndAvatar(token *oauth2.Token) (string, string) {
|
||||
type GithubUser struct {
|
||||
Login string `json:"login"`
|
||||
AvatarUrl string `json:"avatar_url"`
|
||||
}
|
||||
var githubUser GithubUser
|
||||
var githubUserInfo GitHubUserInfo
|
||||
|
||||
req, err := http.NewRequest("GET", "https://api.github.com/user", nil)
|
||||
if err != nil {
|
||||
@ -116,14 +207,20 @@ func (idp *GithubIdProvider) getLoginAndAvatar(token *oauth2.Token) (string, str
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
contents2, err := ioutil.ReadAll(resp.Body)
|
||||
err = json.Unmarshal(contents2, &githubUser)
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return githubUser.Login, githubUser.AvatarUrl
|
||||
err = json.Unmarshal(body, &githubUserInfo)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return githubUserInfo.Login, githubUserInfo.AvatarUrl
|
||||
}
|
||||
|
||||
func (idp *GithubIdProvider) GetUserInfo(token *oauth2.Token) (*UserInfo, error) {
|
||||
|
Reference in New Issue
Block a user