Esempio n. 1
0
func InstallAuthBounce(w http.ResponseWriter, r *http.Request) {
	userTokens := r.URL.Query()["userToken"]

	userToken := ""

	if len(userTokens) > 0 {
		userToken = userTokens[0]
	}

	var session *sessions.Session
	if err := pc.GetSession(cookieJar, r, &session); err != nil {
		http.Error(w, err.Message, err.HttpStatus)
		return
	}

	session.Values["INSTALL_USER_TOKEN"] = userToken

	if err := cookieJar.Save(r, w, session); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	url := installOAuth2Config.Config.AuthCodeURL(
		installOAuth2Config.State,
		oauth2.AccessTypeOffline,
		//		oauth2.ApprovalForce,
	)

	http.Redirect(w, r, url, http.StatusFound)
}
Esempio n. 2
0
func doInstall(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	dec := schema.NewDecoder()

	var authResp AuthResponse

	err := dec.Decode(&authResp, r.Form)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	var session *sessions.Session

	if err := pc.GetSession(cookieJar, r, &session); err != nil {
		http.Error(w, err.Message, err.HttpStatus)
		return
	}

	userToken, ok := session.Values["INSTALL_USER_TOKEN"].(string)
	if !ok {
		http.Error(w, "Could not find user token in session", http.StatusBadRequest)
		return
	}

	authResp.UserToken = userToken
	session.Values["INSTALL_USER_TOKEN"] = nil
	session.Values["INSTALL_AUTH_RESPONSE"] = &authResp

	if err := cookieJar.Save(r, w, session); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	http.Redirect(w, r, redirUrlBase.String()+"#/install", http.StatusFound)
}
Esempio n. 3
0
func InstallUser(w http.ResponseWriter, r *http.Request) (interface{}, *pc.RestError) {
	var ginfo map[string]interface{}

	var session *sessions.Session

	if err := pc.GetSession(cookieJar, r, &session); err != nil {
		return nil, err
	}
	authResp, ok := session.Values["INSTALL_AUTH_RESPONSE"].(*AuthResponse)
	if !ok {
		return nil, &pc.RestError{
			Message:    "Bad session, no auth response",
			HttpStatus: http.StatusBadRequest,
		}
	}

	session.Values["INSTALL_AUTH_RESPONSE"] = nil

	if err := checkAuthResponse(authResp, &installOAuth2Config); err != nil {
		return nil, err
	}

	gToken, err := getGoogleToken(authResp, &installOAuth2Config)

	if err != nil {
		return nil, err
	}

	if err := pc.GetGoogleInfoFromToken(gToken.AccessToken, installOAuth2Config.Config, &ginfo); err != nil {
		return nil, err
	}

	if authResp.UserToken == "" {
		return nil, &pc.RestError{
			Message:    "No userToken supplied",
			HttpStatus: http.StatusBadRequest,
		}
	}

	gid := fmt.Sprintf("%v", ginfo["user_id"])
	email := fmt.Sprintf("%v", ginfo["email"])

	tryUpsert := func(username string, retryCount int, user *pc.User) error {
		if retryCount > 0 {
			username = fmt.Sprintf("%s%d", username, retryCount)
		}
		update := mgo.Change{
			Upsert:    true,
			ReturnNew: true,
			Update: bson.M{
				"$setOnInsert": bson.M{
					"creationts":   time.Now(),
					"gid":          gid,
					"uuid":         uuid.NewV4().String(),
					"authtoken":    base64.StdEncoding.EncodeToString(uuid.NewV4().Bytes()),
					"refreshtoken": base64.StdEncoding.EncodeToString(uuid.NewV4().Bytes()),
				},
				"$set": bson.M{
					"email":    email,
					"username": username,
					"ginfo":    ginfo,
					"gtoken":   gToken,
				},
			},
		}
		if _, err := userC.Find(bson.M{"gid": gid}).Apply(update, user); err != nil {
			return err
		}

		return nil
	}

	username := strings.ToLower(strings.Split(email, "@")[0])
	username = sanitizeUsernameRegex.ReplaceAllLiteralString(username, "")
	for retryCount := 0; retryCount < 1000; retryCount++ {
		var user pc.User
		if err := tryUpsert(username, retryCount, &user); err != nil {
			if mgo.IsDup(err) && strings.Contains(err.Error(), "username") {
				continue
			}

			return nil, &pc.RestError{
				WrappedException: err,
				Message:          "Error creating user account",
				HttpStatus:       http.StatusInternalServerError,
			}
		}

		//Use "user" object

		if _, err := pc.CreateMirrorAccount(
			user,
			authResp.UserToken,
			*baseJwtConfig,
		); err != nil {
			return nil, &pc.RestError{
				WrappedException: err,
				Message:          "Error creating mirror account",
				HttpStatus:       http.StatusInternalServerError,
			}

		}

		return user, nil
	}

	return nil, &pc.RestError{
		Message:    "Error creating user account",
		HttpStatus: http.StatusInternalServerError,
	}

}