(c *gin.Context)
| 79 | } |
| 80 | |
| 81 | func GitHubOAuth(c *gin.Context) { |
| 82 | session := sessions.Default(c) |
| 83 | state := c.Query("state") |
| 84 | if state == "" || session.Get("oauth_state") == nil || state != session.Get("oauth_state").(string) { |
| 85 | c.JSON(http.StatusForbidden, gin.H{ |
| 86 | "success": false, |
| 87 | "message": "state is empty or not same", |
| 88 | }) |
| 89 | return |
| 90 | } |
| 91 | username := session.Get("username") |
| 92 | if username != nil { |
| 93 | GitHubBind(c) |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | if !common.GitHubOAuthEnabled { |
| 98 | c.JSON(http.StatusOK, gin.H{ |
| 99 | "success": false, |
| 100 | "message": "管理员未开启通过 GitHub 登录以及注册", |
| 101 | }) |
| 102 | return |
| 103 | } |
| 104 | code := c.Query("code") |
| 105 | githubUser, err := getGitHubUserInfoByCode(code) |
| 106 | if err != nil { |
| 107 | common.ApiError(c, err) |
| 108 | return |
| 109 | } |
| 110 | user := model.User{ |
| 111 | GitHubId: githubUser.Login, |
| 112 | } |
| 113 | // IsGitHubIdAlreadyTaken is unscoped |
| 114 | if model.IsGitHubIdAlreadyTaken(user.GitHubId) { |
| 115 | // FillUserByGitHubId is scoped |
| 116 | err := user.FillUserByGitHubId() |
| 117 | if err != nil { |
| 118 | c.JSON(http.StatusOK, gin.H{ |
| 119 | "success": false, |
| 120 | "message": err.Error(), |
| 121 | }) |
| 122 | return |
| 123 | } |
| 124 | // if user.Id == 0 , user has been deleted |
| 125 | if user.Id == 0 { |
| 126 | c.JSON(http.StatusOK, gin.H{ |
| 127 | "success": false, |
| 128 | "message": "用户已注销", |
| 129 | }) |
| 130 | return |
| 131 | } |
| 132 | } else { |
| 133 | if common.RegisterEnabled { |
| 134 | user.Username = "github_" + strconv.Itoa(model.GetMaxUserId()+1) |
| 135 | if githubUser.Name != "" { |
| 136 | user.DisplayName = githubUser.Name |
| 137 | } else { |
| 138 | user.DisplayName = "GitHub User" |
nothing calls this directly
no test coverage detected