mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-31 20:02:09 +00:00
Add weibo oauth
This commit is contained in:
parent
4b9b8024ba
commit
d2b53dd43b
20 changed files with 198 additions and 21 deletions
|
@ -48,7 +48,7 @@ func NewOauthService() {
|
|||
base.OauthService.OauthInfos = make(map[string]*base.OauthInfo)
|
||||
|
||||
socialConfigs := make(map[string]*oauth.Config)
|
||||
allOauthes := []string{"github", "google", "qq", "twitter"}
|
||||
allOauthes := []string{"github", "google", "qq", "twitter", "weibo"}
|
||||
// Load all OAuth config data.
|
||||
for _, name := range allOauthes {
|
||||
base.OauthService.OauthInfos[name] = &base.OauthInfo{
|
||||
|
@ -98,6 +98,13 @@ func NewOauthService() {
|
|||
enabledOauths = append(enabledOauths, "Twitter")
|
||||
}
|
||||
|
||||
// Weibo.
|
||||
if base.Cfg.MustBool("oauth.weibo", "ENABLED") {
|
||||
base.OauthService.Weibo = true
|
||||
newWeiboOauth(socialConfigs["weibo"])
|
||||
enabledOauths = append(enabledOauths, "Weibo")
|
||||
}
|
||||
|
||||
log.Info("Oauth Service Enabled %s", enabledOauths)
|
||||
}
|
||||
|
||||
|
@ -331,3 +338,56 @@ func (s *SocialTwitter) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo
|
|||
// }, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// __ __ ._____.
|
||||
// / \ / \ ____ |__\_ |__ ____
|
||||
// \ \/\/ // __ \| || __ \ / _ \
|
||||
// \ /\ ___/| || \_\ ( <_> )
|
||||
// \__/\ / \___ >__||___ /\____/
|
||||
// \/ \/ \/
|
||||
|
||||
type SocialWeibo struct {
|
||||
Token *oauth.Token
|
||||
*oauth.Transport
|
||||
}
|
||||
|
||||
func (s *SocialWeibo) Type() int {
|
||||
return models.OT_WEIBO
|
||||
}
|
||||
|
||||
func newWeiboOauth(config *oauth.Config) {
|
||||
SocialMap["weibo"] = &SocialWeibo{
|
||||
Transport: &oauth.Transport{
|
||||
Config: config,
|
||||
Transport: http.DefaultTransport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SocialWeibo) SetRedirectUrl(url string) {
|
||||
s.Transport.Config.RedirectURL = url
|
||||
}
|
||||
|
||||
func (s *SocialWeibo) UserInfo(token *oauth.Token, _ *url.URL) (*BasicUserInfo, error) {
|
||||
transport := &oauth.Transport{Token: token}
|
||||
var data struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
var err error
|
||||
|
||||
reqUrl := "https://api.weibo.com/2/users/show.json"
|
||||
r, err := transport.Client().Get(reqUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &BasicUserInfo{
|
||||
Identity: data.Id,
|
||||
Name: data.Name,
|
||||
}, nil
|
||||
return nil, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue