middleware

package
v0.0.0-...-f26991d Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2015 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const CSRF_TOKEN_HEADER_NAME = "X-WCG-CSRF-Token"
View Source
const CSRF_TOKEN_PARAM_NAME = "__csrf_token"
View Source
const LOCAL_OAUTH2_CLIENT = "oauth2_client"

Variables

View Source
var DefaultAuthorizedHandler = func(res *Response, req *Request) {
	res.WriteString("Authorized.")
	res.End()
}
View Source
var DefaultInvalidatedHandler = func(res *Response, req *Request) {
	res.WriteHeader(403)
	res.WriteString("Invalidated.")
	res.End()
}
View Source
var DefaultParameterName = "code"
View Source
var DefaultTransportFactory = func(res *Response, req *Request) http.RoundTripper {
	return http.DefaultTransport
}
View Source
var DefaultUnauthorizedHandler = func(res *Response, req *Request) {
	res.WriteHeader(403)
	res.WriteString("Unauthorized.")
	res.End()
}
View Source
var ErrCodeIsEmpty = errors.New("code parameter is empty.")
View Source
var ErrSessionNotFound = errors.New("Session support is required but not configured.")
View Source
var ErrTokenExchange = errors.New("token exchange failed.")
View Source
var SessionConfigIni = &SessionConfig{
	StoreFactory: nil,
	Key:          "wcgsess",
	MaxAge:       7776000,
	CookieName:   "wcgsess",
	Domain:       "",
	HttpOnly:     true,
	Path:         "/",
}

Functions

func AccessLog

func AccessLog(out io.Writer, format string) func(*Response, *Request)

Write access logs into `out` writer with the specified `format`. You can use the following placeholder variables in format string with '$var'.

  • IP ip address of the client.
  • User an authenticated user name
  • Time access time like '02/Nov/2013:17:29:48 +0900'
  • Method http method
  • Path path string including query string.
  • Version version string such as 'HTTP/1.1'
  • Status HTTP status code number
  • Size response body size.
  • Referer referer string
  • Agent user agent string
  • ResponseTime seconds taken for the response.

if format string is "", it would be "$IP - $User [$Time] \"$Method $Path $Version\" $Status $Size \"$Referer\" \"$Agent\" $ResponseTime".

func CSRFSupport

func CSRFSupport() func(*Response, *Request)

Adding a helper method csrf() to generate a hidden input for CSRF, and returns validator handler

func OAuth2

func OAuth2(cfg *OAuth2Config) (
	func(*Response, *Request), func(*Response, *Request),
	func(*Response, *Request), func(*Response, *Request))

Returns a list of middleware functions.

  • 1st: would be a middleware that redicts to the authorize URL.
  • 2nd: would be a middleware that serves the callback from OAuth URL. if the client authorize the access, it would call AuthorizedHandler after registering the token with the session. otherwise, it would call UnauthorizedHandler.
  • 3rd: would be a middleware that checks the token stored in the session and validate it. if the token is not found or invalidated, it would call InvalidateHandler.
  • 4th: would be a middleware that perform logout by cleaning up the current oauth token from a sessions

This support function also adds some view helpers on your view system.

oauth2_by(providerName string) : returns true if the user is authoriezed

func SessionSupport

func SessionSupport() (func(*Response, *Request), func(*Response, *Request))

Returns 2 middleware functions for session support, which need to be registered on route.Before and route.After.

Example:

sprepare, scomplete := SessionSupport()

route.Before(sprepare)
route.After(scomplete)

func SessionSupportWithConfig

func SessionSupportWithConfig(cfg *SessionConfig) (func(*Response, *Request), func(*Response, *Request))

func StaticFile

func StaticFile(prefix string, dir string) func(*Response, *Request)

Static File hosting for prefix path.

Types

type OAuth2Config

type OAuth2Config struct {
	oauth.Config
	ParameterName       string
	UnauthorizedHandler func(*Response, *Request)
	AuthorizedHandler   func(*Response, *Request)
	InvalidatedHandler  func(*Response, *Request)
	TransportFactory    func(*Response, *Request) http.RoundTripper
	UserFactory         func(*Response, *Request) (*OAuth2User, error)
}

OAuth2Config is a configuration for oauth2 http interaction.

func (*OAuth2Config) Use

func (r *OAuth2Config) Use(req *Request) *oauth.Config

Return the configuration for redirect url context.

type OAuth2User

type OAuth2User struct {
	*oauth.Token
	// contains filtered or unexported fields
}

AuthorizedUser

func NewOAuth2User

func NewOAuth2User(id string, name string, profile string, image string, provider string) *OAuth2User

func (*OAuth2User) AuthProvider

func (u *OAuth2User) AuthProvider() string

func (*OAuth2User) DisplayName

func (u *OAuth2User) DisplayName() string

func (*OAuth2User) Id

func (u *OAuth2User) Id() string
func (u *OAuth2User) ImageLink() string

func (*OAuth2User) LastLogin

func (u *OAuth2User) LastLogin() time.Time
func (u *OAuth2User) ProfileLink() string

type SessionConfig

type SessionConfig struct {
	StoreFactory func(r *Request) SessionStore
	Key          string `ini:"key" default:"wcgsess"`         // Signature key for session id.
	MaxAge       int    `ini:"max_age" default:"7776000"`     // maxage of the sesion cookie
	CookieName   string `ini:"cookie_name" default:"wcgsess"` // name of the session cookie.
	Domain       string `ini:"domain" default:""`             // domain of the session cookie. Host header value would be used if it is ""
	HttpOnly     bool   `ini:"http_only" default:true`        // httponly of the session cookie
	Path         string `ini:"path" defualt:"/"`              // Cookie Path
}

Directories

Path Synopsis
oauth2providers

Jump to

Keyboard shortcuts

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