Skip to content

API reference

Greg Bowler edited this page Jul 20, 2020 · 1 revision

All core symbols are available from github.com/Authwave/go-client. Start with Quick start guide for a complete setup.

Authenticator

auth, err := authwave.NewAuthenticator(authwave.Options{
    ClientKey: clientKey, CurrentURIPath: requestPath,
    AuthwaveHost: "login.authwave.com", Session: session,
    RedirectHandler: nil, OnAuthenticated: nil,
})

Construction restores stored identity and processes incoming responses. OnAuthenticated is func(*AuthUser) error. The client can redirect during construction.

Method Behavior
IsLoggedIn() bool Reports whether this instance has a user.
GetAuthUser() (*AuthUser, error) Returns the identity or a not-logged-in error.
Login(token *Token) error Stores token and redirects; no-op when logged in.
Logout(redirectTo *string, token *Token) error Clears identity and starts provider logout.
SetReturnURI(uri string) Sets subsequent login and default logout destinations.
GetLoginURI(token *Token) (string, error) Builds a URI without storing or redirecting.
GetAdminURI() (string, error) Returns provider administration URI.
FakeLogin(userID string, email *string, redirectTo string) error Development-only synthetic login.
GetUser() (*User, error) Deprecated compatibility getter.

AuthUser

NewAuthUser(id, email) returns an immutable identity snapshot accessed through ID() and Email(). User is a deprecated type alias, and GetUser() is a deprecated getter.

Session and redirect contracts

NewGlobalSessionContainer(map[string]any) returns a container or error. SessionContainer defines Get(key) (any, error), Set(key, value) error, Contains(key) bool, Remove(key) error.

NewSessionData(token, data, returnQuery) provides GetToken(), GetData(), GetReturnQuery(), HasInvokedAuthenticationCallback() and MarkAuthenticationCallbackInvoked(). JSON persistence restores its token and response. Missing token or data returns ErrNotLoggedIn.

RedirectHandler is func(uri string, code int) error; the default returns *RedirectError with URI and Code. Return it to the HTTP adapter.

NewHTTPHandler(HTTPOptions, HTTPHandler) returns http.Handler and error. The callback takes (*Authenticator, *http.Request) and returns (*Response, error). Response contains Status, Header, Body. HTTPOptions accepts ClientKey, AuthwaveHost, OnAuthenticated, required Store, CookieName, InsecureCookie (default false) and TTL (default 24 hours). See Sessions and redirects for the store contract.

Supporting exchange objects

NewToken(secret) (*Token, error) generates nonces. NewTokenWithIVs(secret, sessionIV, iv) accepts fixed InitVector values. Token methods are GetIV(), GetSecretIV(), GenerateRequestCipher(message) and Decode(base64cipher). NewInitVector() generates 24 random bytes; WithBytes() returns a replaced copy. Nonces and ciphertext expose base64 String() and defensive GetBytes().

NewLoginURI(token, currentPath, deploymentID, host), NewLogoutURI(token, currentPath, deploymentID, host, redirectTo) and NewAdminURI(host) return a URI string and error. An empty provider host uses the default.

NewUserResponseData(id, email, message) exposes GetID(), GetEmail() and inherited GetMessage(). The low-level Token.Decode() uses the request IV and accepts uuid or id; normal authentication uses the session IV and id. Both read email and ignore extra fields.

Exceptions

Use errors.Is with ErrSessionNotStarted, ErrNotLoggedIn, ErrInsecureProtocol, ErrInvalidUserDataSerialization, ErrEncryptionFailure and ErrDecryptionFailure. ErrResponseCipherDecryption is a compatibility declaration; cipher failures use ErrDecryptionFailure. errors.As detects *RedirectError. Callback errors propagate unchanged.

Clone this wiki locally