Beispiel #1
0
// Get user two factor data
func GetUser2fa(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	jr := jresp.NewJsonResp()
	if !authUser(r) {
		jr.Error("Not authorized")
		fmt.Fprint(w, jr.ToString(debug))
		return
	}

	// User
	user := getUser(r)
	if user.HasTwoFactor() {
		jr.Error("Two factor authentication already setup")
		fmt.Fprint(w, jr.ToString(debug))
		return
	}

	// Create TOTP conf
	secret := TotpSecret()
	cotp := dgoogauth.OTPConfig{
		Secret:     secret,
		WindowSize: TOTP_MAX_WINDOWS,
	}

	// Image uri
	qrCodeImageUri := cotp.ProvisionURI(fmt.Sprintf("indispenso:%s", user.Username))

	// QR code
	qrCode, qrErr := qr.Encode(qrCodeImageUri, qr.H)
	if qrErr != nil {
		jr.Error("Failed to generate QR code")
		fmt.Fprint(w, jr.ToString(debug))
		return
	}

	// Save user, not yet enabled
	user.TotpSecret = secret
	server.userStore.save()

	jr.Set("Secret", user.TotpSecret)
	jr.Set("Png", qrCode.PNG())
	jr.OK()
	fmt.Fprint(w, jr.ToString(debug))
}