/
githubController.go
51 lines (47 loc) · 1.2 KB
/
githubController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @Author: zhenwei zhang
* @Date: 2018-03-29 18:20:28
* @Last Modified by: zhenwei zhang
* @Last Modified time: 2018-06-24 13:17:09
*/
package controllers
import (
"strings"
"errors"
"github.com/astaxie/beego"
"github.com/virskor/beegoStart/oauth"
)
type GithubController struct {
BaseController
}
func (this *GithubController) Login() {
var (
ierr error
)
githubClientId := beego.AppConfig.String("oauth::githubClientId_" + env )
githubClientCallback := beego.AppConfig.String("oauth::githubClientCallback_" + env)
code := this.GetString("code")
if strings.Trim(code, "") == ""{
redirectUrl := "https://github.com/login/oauth/authorize?client_id=" + githubClientId + "&redirect_uri=" + githubClientCallback
this.Redirect(redirectUrl, 302)
return
}
if token, err := oauth.GetGithubAccessToken(code, env); err != nil {
ierr = err
} else {
if info, err := oauth.GetGithubUserInfo(token.AccessToken); err != nil {
ierr = err
} else {
if info.Id > 0 {
this.SetSession("githubUser",&info)
} else {
ierr = errors.New("获取github用户数据失败")
}
}
}
if ierr != nil {
this.Ctx.WriteString(ierr.Error())
}else{
this.Redirect("../../user/bind/", 302)
}
}