Documentation
¶
Overview ¶
Package oauthpopup runs a provider sign-in in a popup and hands the new session to the page that opened it. It is for an app embedded on another site (an iframe in a customer's page), where the browser partitions or blocks third-party cookies: the popup is a first-party window on the auth origin, so the sign-in completes there, and the completion page posts the session token to the opener, which then sends it as Authorization: Bearer.
It lives beside oauthproxy because both are opt-in browser-flow companions of plugin/oauth with no dependency of their own; it is its own package because it shares nothing with the proxy, and an app imports only the one it uses.
The flow:
GET <mount>/start?provider=google&popupOrigin=https://app.customer.com&popupNonce=…
checks popupOrigin is trusted, sets a sealed marker cookie (origin,
nonce, time), and starts the oauth plugin's flow with this package's
completion page as returnTo and errorTo.
GET <mount>/complete
opens and expires the marker; hands out only a session created after
the marker, so a page can never fish an existing session out of the
cookie; renders a page whose fixed script posts {type, nonce, token,
redirectTo, error} to the opener at popupOrigin and closes.
The token is a bearer credential: any script on the opener's origin can read it, which is the point and the risk. Keep such sessions short.
Index ¶
Constants ¶
const ( // MessageType is the type of the posted message. //ds:def id=oauthpopup-messagetype-3w8kq5nv owner=@khanakia stability=stable MessageType = "ubgo-auth:oauth-popup" // DataElementID is the id of the inert JSON element on the page. DataElementID = "ubgo-auth-oauth-popup" // MarkerCookie holds the sealed marker between start and completion. MarkerCookie = "__Host-auth_popup" // DefaultMount is where the app mounts Handler: the oauth plugin's mount // prefix and this plugin's name. DefaultMount = oauth.DefaultMountPrefix + "/" + PluginName // MarkerTTL is how long a popup sign-in may take, 10 minutes. //ds:def id=oauthpopup-markerttl-5q2xn7wk owner=@khanakia stability=stable MarkerTTL = 10 * time.Minute )
Names that cross to the browser.
const ( ErrorSignInFailed = "popup_sign_in_failed" ErrorBadCallback = "invalid_callback_url" ErrorBadErrorURL = "invalid_error_callback_url" ErrorBadNewUser = "invalid_new_user_callback_url" ErrorNoProvider = "provider_not_found" )
Error codes the page posts.
const PluginName = "oauthpopup"
PluginName is the plugin's permanent identity.
Variables ¶
var ErrUntrustedOrigin = auth.NewError("oauthpopup.untrusted_origin", http.StatusForbidden, "This page may not open a sign-in popup.")
ErrUntrustedOrigin answers a start whose popupOrigin the app does not trust.
var ScriptCSPHash = func() string { h := sha256.Sum256([]byte(completionScript)) return "'sha256-" + base64.StdEncoding.EncodeToString(h[:]) + "'" }()
ScriptCSPHash is the CSP source of completionScript.
Functions ¶
Types ¶
type Option ¶
type Option func(*Plugin)
Option configures the plugin.
func HTTPOptions ¶
HTTPOptions are the authhttp options the app's handler uses (cookie prefix, trusted proxies), so this handler reads the same cookies.
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin is the popup plugin. Build it with New.
func (*Plugin) Handler ¶
Handler serves start and complete. Mount it with the prefix stripped:
mux.Handle(oauthpopup.DefaultMount+"/", http.StripPrefix(oauthpopup.DefaultMount, pp.Handler()))
func (*Plugin) RewriteCallback ¶
RewriteCallback is an after hook on the provider callback: wrap the handler that serves the oauth plugin's routes with it, and a callback that ends a popup sign-in (the marker cookie is present and the callback redirects to this plugin's completion page) answers with the completion page itself instead of the redirect, carrying the session the callback just issued, or its error. Every other response passes through unchanged.
mux.Handle("/auth/", pp.RewriteCallback(authhttp.Handler(a, authhttp.Prefix("/auth"))))
It saves the browser one request and changes nothing else: the page is built by the same code as GET <mount>/complete, from the cookies the callback set, so it still spends the marker and still hands out only a session created after the marker. Without it the redirect reaches /complete, with the same outcome. Responses are buffered only while a marker cookie is present.