-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support Github Login * improve according to codefactor * fix due to last updates * optimization Co-authored-by: Noah Hsu <i@nn.ci>
- v3.42.0
- v3.41.0
- v3.40.0
- v3.39.4
- v3.39.3
- v3.39.2
- v3.39.1
- v3.38.0
- v3.37.4
- v3.37.3
- v3.37.2
- v3.37.1
- v3.36.0
- v3.35.0
- v3.34.0
- v3.33.0
- v3.32.0
- v3.31.0
- v3.30.0
- v3.29.1
- v3.29.0
- v3.28.0
- v3.27.0
- v3.26.0
- v3.25.1
- v3.24.0
- v3.23.0
- v3.22.1
- v3.22.0
- v3.21.0
- v3.20.1
- v3.20.0
- v3.19.0
- v3.18.0
- v3.17.0
- v3.16.3
- v3.16.2
- v3.16.1
- v3.16.0
- v3.15.1
- v3.15.0
- v3.14.0
- v3.13.2
- v3.13.1
- v3.12.2
- v3.12.1
- v3.12.0
- v3.11.0
- v3.10.1
- v3.9.2
- v3.9.1
- v3.9.0
- v3.8.0
- v3.7.2
- v3.7.1
- beta
1 parent
c00dcc8
commit 83fe17c
Showing
8 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ const ( | |
GLOBAL | ||
ARIA2 | ||
INDEX | ||
GITHUB | ||
) | ||
|
||
const ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package handles | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
"strconv" | ||
|
||
"github.com/alist-org/alist/v3/internal/db" | ||
"github.com/alist-org/alist/v3/pkg/utils" | ||
"github.com/alist-org/alist/v3/server/common" | ||
"github.com/gin-gonic/gin" | ||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
func GithubLoginRedirect(c *gin.Context) { | ||
method := c.Query("method") | ||
callbackURL := c.Query("callback_url") | ||
withParams := c.Query("with_params") | ||
enabled, err := db.GetSettingItemByKey("github_login_enabled") | ||
clientId, err := db.GetSettingItemByKey("github_client_id") | ||
if err != nil { | ||
common.ErrorResp(c, err, 400) | ||
return | ||
} else if enabled.Value == "true" { | ||
urlValues := url.Values{} | ||
urlValues.Add("client_id", clientId.Value) | ||
if method == "get_github_id" { | ||
urlValues.Add("allow_signup", "true") | ||
} else if method == "github_callback_login" { | ||
urlValues.Add("allow_signup", "false") | ||
} | ||
if method == "" { | ||
common.ErrorStrResp(c, "no method provided", 400) | ||
return | ||
} | ||
if withParams != "" { | ||
urlValues.Add("redirect_uri", common.GetApiUrl(c.Request)+"/api/auth/github_callback"+"?method="+method+"&callback_url="+callbackURL+"&with_params="+withParams) | ||
} else { | ||
urlValues.Add("redirect_uri", common.GetApiUrl(c.Request)+"/api/auth/github_callback"+"?method="+method+"&callback_url="+callbackURL) | ||
} | ||
c.Redirect(302, "https://github.com/login/oauth/authorize?"+urlValues.Encode()) | ||
} else { | ||
common.ErrorStrResp(c, "github Login not enabled", 403) | ||
} | ||
} | ||
|
||
var githubClient = resty.New().SetRetryCount(3) | ||
|
||
func GithubLoginCallback(c *gin.Context) { | ||
argument := c.Query("method") | ||
callbackUrl := c.Query("callback_url") | ||
if argument == "get_github_id" || argument == "github_login" { | ||
enabled, err := db.GetSettingItemByKey("github_login_enabled") | ||
clientId, err := db.GetSettingItemByKey("github_client_id") | ||
clientSecret, err := db.GetSettingItemByKey("github_client_secrets") | ||
if err != nil { | ||
common.ErrorResp(c, err, 400) | ||
return | ||
} else if enabled.Value == "true" { | ||
callbackCode := c.Query("code") | ||
if callbackCode == "" { | ||
common.ErrorStrResp(c, "No code provided", 400) | ||
return | ||
} | ||
resp, err := githubClient.R().SetHeader("content-type", "application/json"). | ||
SetBody(map[string]string{ | ||
"client_id": clientId.Value, | ||
"client_secret": clientSecret.Value, | ||
"code": callbackCode, | ||
"redirect_uri": common.GetApiUrl(c.Request) + "/api/auth/github_callback", | ||
}).Post("https://github.com/login/oauth/access_token") | ||
if err != nil { | ||
common.ErrorResp(c, err, 400) | ||
return | ||
} | ||
accessToken := utils.Json.Get(resp.Body(), "access_token").ToString() | ||
resp, err = githubClient.R().SetHeader("Authorization", "Bearer "+accessToken). | ||
Get("https://api.github.com/user") | ||
ghUserID := utils.Json.Get(resp.Body(), "id").ToInt() | ||
if argument == "get_github_id" { | ||
c.Redirect(302, callbackUrl+"?githubID="+strconv.Itoa(ghUserID)) | ||
} | ||
if argument == "github_login" { | ||
user, err := db.GetUserByGithubID(ghUserID) | ||
if err != nil { | ||
common.ErrorResp(c, err, 400) | ||
} | ||
token, err := common.GenerateToken(user.Username) | ||
withParams := c.Query("with_params") | ||
if withParams == "true" { | ||
c.Redirect(302, callbackUrl+"&token="+token) | ||
} else if withParams == "false" { | ||
c.Redirect(302, callbackUrl+"?token="+token) | ||
} | ||
return | ||
} | ||
} else { | ||
common.ErrorResp(c, errors.New("invalid request"), 500) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters