func signInHandler(ctx *app.Context) { modal := ctx.FormValue("modal") != "" d := data(ctx) if !modal && !d.allowDirectSignIn() && d.hasEnabledSocialSignin() { // Redirect to the only available social sign-in ctx.MustRedirectReverse(false, d.enabledSocialAccountTypes()[0].HandlerName) return } from := ctx.FormValue(app.SignInFromParameterName) signIn := SignIn{From: from} form := form.New(ctx, &signIn) if d.allowDirectSignIn() && form.Submitted() && form.IsValid() { ctx.MustSignIn(asGondolaUser(reflect.ValueOf(signIn.User))) ctx.RedirectBack() return } user, _ := newEmptyUser(ctx) data := map[string]interface{}{ "SocialAccountTypes": d.enabledSocialAccountTypes(), "From": from, "SignInForm": form, "SignUpForm": SignUpForm(ctx, user), "AllowDirectSignIn": d.allowDirectSignIn(), "AllowRegistration": d.allowRegistration(), } tmpl := SignInTemplateName if modal && SignInModalTemplateName != "" { tmpl = SignInModalTemplateName } ctx.MustExecute(tmpl, data) }
func redirectToFrom(ctx *app.Context) { from := ctx.FormValue(app.SignInFromParameterName) if from == "" { from = "/" } ctx.Redirect(from, false) }
func windowCallbackHandler(ctx *app.Context, user reflect.Value, callback string) { inWindow := ctx.FormValue("window") != "" if user.IsValid() { ctx.MustSignIn(asGondolaUser(user)) } if inWindow { var payload []byte if user.IsValid() { var err error payload, err = JSONEncode(ctx, user.Interface()) if err != nil { panic(err) } } ctx.MustExecute("js-callback.html", map[string]interface{}{ "Callback": callback, "Payload": payload, }) } else { if user.IsValid() { redirectToFrom(ctx) } else { ctx.MustRedirectReverse(false, app.SignInHandlerName) } } }
func (r *reCaptcha) responseIsValid(ctx *app.Context) (bool, string) { challenge := ctx.FormValue("recaptcha_challenge_field") response := ctx.FormValue("recaptcha_response_field") values := url.Values{ "privatekey": {r.privateKey}, "remoteip": {ctx.RemoteAddress()}, "challenge": {challenge}, "response": {response}, } resp, err := http.PostForm("http://www.google.com/recaptcha/api/verify", values) if err == nil { defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) if err == nil { lines := strings.Split(string(b), "\n") if len(lines) > 0 { if lines[0] == "true" { return true, "" } if len(lines) > 1 { return false, lines[1] } } } } return false, "" }
func jsSignInFacebookHandler(ctx *app.Context) { req := ctx.FormValue("req") fbApp := data(ctx).opts.FacebookApp.Clone(ctx) resp, err := fbApp.ParseSignedRequest(req) if err != nil { panic(err) } // Let it crash if the data does not have the // specified format, this will make it easier // to find it if it happens. code := resp["code"].(string) token, err := fbApp.Exchange("", code) user, err := userFromFacebookToken(ctx, token) if err != nil { panic(err) } ctx.MustSignIn(asGondolaUser(user)) writeJSONEncoded(ctx, user) }
func ResetHandler(ctx *app.Context) { d := data(ctx) if !d.allowDirectSignIn() { ctx.NotFound("") return } payload := ctx.FormValue("p") var valid bool var expired bool var f *form.Form var user reflect.Value var err error var done bool if payload != "" { user, err = decodeResetPayload(ctx, payload) if err == nil && user.IsValid() { valid = true } else { if err == errResetExpired { expired = true } } } if valid { passwordForm := &PasswordForm{User: user} f = form.New(ctx, passwordForm) if f.Submitted() && f.IsValid() { ctx.Orm().MustSave(user.Interface()) ctx.MustSignIn(asGondolaUser(user)) done = true } } data := map[string]interface{}{ "Valid": valid, "Expired": expired, "Done": done, "User": user, "PasswordForm": f, "Payload": payload, } ctx.MustExecute(ResetTemplateName, data) }
func signUpHandler(ctx *app.Context) { d := data(ctx) if !d.allowDirectSignIn() { ctx.NotFound("") return } from := ctx.FormValue(app.SignInFromParameterName) user, _ := newEmptyUser(ctx) form := SignUpForm(ctx, user) if form.Submitted() && form.IsValid() { saveNewUser(ctx, user) ctx.RedirectBack() return } data := map[string]interface{}{ "From": from, "SignUpForm": form, } ctx.MustExecute(SignUpTemplateName, data) }