Documentation
¶
Index ¶
- Constants
- Variables
- func AccessLog(out io.Writer, format string) func(*Response, *Request)
- func CSRFSupport() func(*Response, *Request)
- func OAuth2(cfg *OAuth2Config) (func(*Response, *Request), func(*Response, *Request), ...)
- func SessionSupport() (func(*Response, *Request), func(*Response, *Request))
- func SessionSupportWithConfig(cfg *SessionConfig) (func(*Response, *Request), func(*Response, *Request))
- func StaticFile(prefix string, dir string) func(*Response, *Request)
- type OAuth2Config
- type OAuth2User
- type SessionConfig
Constants ¶
const CSRF_TOKEN_HEADER_NAME = "X-WCG-CSRF-Token"
const CSRF_TOKEN_PARAM_NAME = "__csrf_token"
const LOCAL_OAUTH2_CLIENT = "oauth2_client"
Variables ¶
var DefaultAuthorizedHandler = func(res *Response, req *Request) {
res.WriteString("Authorized.")
res.End()
}
var DefaultInvalidatedHandler = func(res *Response, req *Request) {
res.WriteHeader(403)
res.WriteString("Invalidated.")
res.End()
}
var DefaultParameterName = "code"
var DefaultTransportFactory = func(res *Response, req *Request) http.RoundTripper { return http.DefaultTransport }
res.WriteHeader(403)
res.WriteString("Unauthorized.")
res.End()
}var ErrCodeIsEmpty = errors.New("code parameter is empty.")
var ErrSessionNotFound = errors.New("Session support is required but not configured.")
var ErrTokenExchange = errors.New("token exchange failed.")
var SessionConfigIni = &SessionConfig{ StoreFactory: nil, Key: "wcgsess", MaxAge: 7776000, CookieName: "wcgsess", Domain: "", HttpOnly: true, Path: "/", }
Functions ¶
func AccessLog ¶
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 ¶
Static File hosting for prefix path.
Types ¶
type OAuth2Config ¶
type OAuth2Config struct { oauth.Config ParameterName string 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 ¶
AuthorizedUser
func NewOAuth2User ¶
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 (*OAuth2User) ImageLink ¶
func (u *OAuth2User) ImageLink() string
func (*OAuth2User) LastLogin ¶
func (u *OAuth2User) LastLogin() time.Time
func (*OAuth2User) ProfileLink ¶
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 }