diff options
author | Unknown <joe2010xtmf@163.com> | 2014-04-13 21:00:12 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-04-13 21:00:12 -0400 |
commit | d2b53dd43b3bc9719985033bc92b76abb9515b4d (patch) | |
tree | f6c038f0b8c11e8a01d7cd26d6bbdb14144696b3 /modules | |
parent | 4b9b8024ba59b5b84d92dca650761b35ebf6408a (diff) | |
download | gitea-d2b53dd43b3bc9719985033bc92b76abb9515b4d.tar.gz gitea-d2b53dd43b3bc9719985033bc92b76abb9515b4d.zip |
Add weibo oauth
Diffstat (limited to 'modules')
-rw-r--r-- | modules/base/conf.go | 2 | ||||
-rw-r--r-- | modules/base/template.go | 22 | ||||
-rw-r--r-- | modules/middleware/repo.go | 7 | ||||
-rw-r--r-- | modules/social/social.go | 62 |
4 files changed, 90 insertions, 3 deletions
diff --git a/modules/base/conf.go b/modules/base/conf.go index 0eca5f4fcb..957ec57b4d 100644 --- a/modules/base/conf.go +++ b/modules/base/conf.go @@ -38,7 +38,7 @@ type OauthInfo struct { // Oauther represents oauth service. type Oauther struct { GitHub, Google, Tencent bool - Twitter bool + Twitter, Weibo bool OauthInfos map[string]*OauthInfo } diff --git a/modules/base/template.go b/modules/base/template.go index 6241497969..79aeeb9d77 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -92,6 +92,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "DiffTypeToStr": DiffTypeToStr, "DiffLineTypeToStr": DiffLineTypeToStr, "ShortSha": ShortSha, + "Oauth2Icon": Oauth2Icon, } type Actioner interface { @@ -109,7 +110,7 @@ func ActionIcon(opType int) string { switch opType { case 1: // Create repository. return "plus-circle" - case 5: // Commit repository. + case 5, 9: // Commit repository. return "arrow-circle-o-right" case 6: // Create issue. return "exclamation-circle" @@ -127,6 +128,7 @@ const ( TPL_CREATE_ISSUE = `<a href="/user/%s">%s</a> opened issue <a href="/%s/issues/%s">%s#%s</a> <div><img src="%s?s=16" alt="user-avatar"/> %s</div>` TPL_TRANSFER_REPO = `<a href="/user/%s">%s</a> transfered repository <code>%s</code> to <a href="/%s">%s</a>` + TPL_PUSH_TAG = `<a href="/user/%s">%s</a> pushed tag <a href="/%s/src/%s">%s</a> at <a href="/%s">%s</a>` ) type PushCommit struct { @@ -174,6 +176,8 @@ func ActionDesc(act Actioner) string { case 8: // Transfer repository. newRepoLink := content + "/" + repoName return fmt.Sprintf(TPL_TRANSFER_REPO, actUserName, actUserName, repoLink, newRepoLink, newRepoLink) + case 9: // Push tag. + return fmt.Sprintf(TPL_PUSH_TAG, actUserName, actUserName, repoLink, branch, branch, repoLink, repoLink) default: return "invalid type" } @@ -197,3 +201,19 @@ func DiffLineTypeToStr(diffType int) string { } return "same" } + +func Oauth2Icon(t int) string { + switch t { + case 1: + return "fa-github-square" + case 2: + return "fa-google-plus-square" + case 3: + return "fa-twitter-square" + case 4: + return "fa-linux" + case 5: + return "fa-weibo" + } + return "" +} diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 1e79ce9870..82c1c2dbf6 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -123,6 +123,13 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { ctx.Repo.GitRepo = gitRepo ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name + tags, err := ctx.Repo.GitRepo.GetTags() + if err != nil { + ctx.Handle(500, "RepoAssignment(GetTags))", err) + return + } + ctx.Repo.Repository.NumTags = len(tags) + ctx.Data["Title"] = user.Name + "/" + repo.Name ctx.Data["Repository"] = repo ctx.Data["Owner"] = user diff --git a/modules/social/social.go b/modules/social/social.go index 230f478fe4..c2ee541776 100644 --- a/modules/social/social.go +++ b/modules/social/social.go @@ -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 +} |