I want Goth to provide a `gothic` package that handles the browser-side authentication flow for registered…
harbor run -t mimo-v2.6-rl/format-code-task-001855harbor download mimo-v2.6-rl/format-code-task-001855Fix the following issue:
I want Goth to provide a gothic package that handles the browser-side authentication flow for registered providers through HTTP request/response objects and a configurable Gorilla sessions.Store.
The package should expose const SessionName = "_gothic_session", a configurable var Store sessions.Store, and initialize Store to an HttpOnly cookie store using SESSION_SECRET when callers do not provide their own store. It should expose BeginAuthHandler(res http.ResponseWriter, req *http.Request), GetAuthURL(res http.ResponseWriter, req *http.Request) (string, error), CompleteUserAuth(res http.ResponseWriter, req *http.Request) (goth.User, error), Logout(res http.ResponseWriter, req *http.Request) error, GetProviderName(req *http.Request) (string, error), GetContextWithProvider(req *http.Request, provider string) *http.Request, SetState(req *http.Request) string, GetState(req *http.Request) string, StoreInSession(key string, value string, req *http.Request, res http.ResponseWriter) error, and GetFromSession(key string, req *http.Request) (string, error).
For a registered provider named faux, calling GetAuthURL with a GET request like /auth?provider=faux should resolve the provider, create a provider auth session with a CSRF state, save the marshaled provider session in Store under the provider name, and return the provider authorization URL. If the request includes state=state_REAL, SetState should use that value; otherwise it should generate a non-empty URL-safe random state, and two separate auth requests should receive different generated state values. BeginAuthHandler should use the same start-auth behavior and respond with 307 Temporary Redirect to the provider authorization URL on success; if no provider can be resolved it should respond with status 400 and write the error text.
Provider names should be resolved from provider or :provider query parameters, Gorilla mux route variables, request context value "provider", chi route params, Go 1.22 PathValue("provider"), GetContextWithProvider, or, as a callback fallback, an existing stored session for any registered provider. If no provider is available, GetProviderName should return the error you must select a provider.
For callback handling, CompleteUserAuth should load the stored provider session, verify that the callback state from the query string or POST form matches the state embedded in the original auth URL, and return state token mismatch on a mismatch. If the stored session can already fetch a user, it should return that goth.User; for example, a stored faux session with name Homer Simpson and email homer@example.com should produce a user with those same fields. If the provider requires authorization on callback, it should authorize the provider session using the query parameters, or POST form values when the POST has no query string, persist the updated session, and then fetch and return the user.
Session helper behavior should be observable: StoreInSession should save a compressed string value under the requested key in SessionName, and GetFromSession should return the original string or the error could not find a matching session for this request when no matching value exists. CompleteUserAuth should clear the temporary login session after a callback, and Logout should also be callable directly, setting the session max age to -1 and clearing all stored values.