state

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

state

State management library

Usage


import "github.com/fbv/state"

// define secret key
const secret = "top_secret"

// generate state and send it to thrid party
func sendRedirect(w http.ResponseWriter, r *http.Request) {
    stateString := state.Generate(secret)
    url := config.AuthCodeURL(stateString)
    log.Println("Redirect to", url)
    http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

// check responce with a state
func handleResponse(w http.ResponseWriter, r *http.Request) {
    stateString := r.FormValue("state")
    if !state.Valid(stateString, secret) {
        log.Printf("Invalid state\n")
        http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
        return
    }
    // state is valid ...
}

See example

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(secret string) string

Generate initializes a new state using random and secret string

func Valid

func Valid(stateString, secret string) bool

Valid checks state

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL