mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-07 01:36:23 +08:00
initial commit
This commit is contained in:
@@ -23,11 +23,6 @@ var github_functions = map[string]interface{}{
|
||||
verify_user: verifyGithubUser,
|
||||
}
|
||||
|
||||
type githubOauthUser struct {
|
||||
Login string `json:"login" bson:"login"`
|
||||
AccessToken string `json:"accesstoken" bson:"accesstoken"`
|
||||
}
|
||||
|
||||
// == handle github authentication here ==
|
||||
|
||||
func initGithub(redirectURL string, clientID string, clientSecret string) {
|
||||
@@ -41,7 +36,7 @@ func initGithub(redirectURL string, clientID string, clientSecret string) {
|
||||
}
|
||||
|
||||
func handleGithubLogin(w http.ResponseWriter, r *http.Request) {
|
||||
var oauth_state_string = logic.RandomString(16)
|
||||
var oauth_state_string = logic.RandomString(user_signin_length)
|
||||
if auth_provider == nil && servercfg.GetFrontendURL() != "" {
|
||||
http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
|
||||
return
|
||||
@@ -61,7 +56,8 @@ func handleGithubLogin(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func handleGithubCallback(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var content, err = getGithubUserInfo(r.URL.Query().Get("state"), r.URL.Query().Get("code"))
|
||||
var rState, rCode = getStateAndCode(r)
|
||||
var content, err = getGithubUserInfo(rState, rCode)
|
||||
if err != nil {
|
||||
logger.Log(1, "error when getting user info from github:", err.Error())
|
||||
http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
|
||||
@@ -93,10 +89,10 @@ func handleGithubCallback(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?login="+jwt+"&user="+content.Login, http.StatusPermanentRedirect)
|
||||
}
|
||||
|
||||
func getGithubUserInfo(state string, code string) (*githubOauthUser, error) {
|
||||
func getGithubUserInfo(state string, code string) (*OAuthUser, error) {
|
||||
oauth_state_string, isValid := logic.IsStateValid(state)
|
||||
if !isValid || state != oauth_state_string {
|
||||
return nil, fmt.Errorf("invalid OAuth state")
|
||||
if (!isValid || state != oauth_state_string) && !isStateCached(state) {
|
||||
return nil, fmt.Errorf("invalid oauth state")
|
||||
}
|
||||
var token, err = auth_provider.Exchange(context.Background(), code)
|
||||
if err != nil {
|
||||
@@ -125,7 +121,7 @@ func getGithubUserInfo(state string, code string) (*githubOauthUser, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed reading response body: %s", err.Error())
|
||||
}
|
||||
var userInfo = &githubOauthUser{}
|
||||
var userInfo = &OAuthUser{}
|
||||
if err = json.Unmarshal(contents, userInfo); err != nil {
|
||||
return nil, fmt.Errorf("failed parsing email from response data: %s", err.Error())
|
||||
}
|
||||
|
Reference in New Issue
Block a user