{"modulePath":"github.com/GoPlugin/pluginV2","version":"v0.5.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sessions\n\n## Constants\n\n```go\nconst (\n\tMaxBcryptPasswordLength = 50\n)\n```\n[https://security.stackexchange.com/questions/39849/does-bcrypt-have-a-maximum-password-length](https://security.stackexchange.com/questions/39849/does-bcrypt-have-a-maximum-password-length)\n\n## Variables\n\n```go\nvar ErrUserSessionExpired = errors.New(\"session missing or expired, please login again\")\n```\nErrUserSessionExpired defines the error triggered when the user session has expired\n\n## Functions\n\n```go\nfunc AddCredentialToUser(o ORM, email string, credential *webauthn.Credential) error\n```\n\n```go\nfunc AuthenticateUserByToken(token *auth.Token, user *User) (bool, error)\n```\nAuthenticateUserByToken returns true on successful authentication of the user against the given Authentication Token.\n\n```go\nfunc BeginWebAuthnLogin(user User, uwas []WebAuthn, sr SessionRequest) (*protocol.CredentialAssertion, error)\n```\n\n```go\nfunc FinishWebAuthnLogin(user User, uwas []WebAuthn, sr SessionRequest) error\n```\n\n```go\nfunc NewSessionReaper(db *sql.DB, config SessionReaperConfig, lggr logger.Logger) utils.SleeperTask\n```\nNewSessionReaper creates a reaper that cleans stale sessions from the store.\n\n```go\nfunc ValidateAndHashPassword(plainPwd string) (string, error)\n```\nValidateAndHashPassword is the single point of logic for user password validations\n\n```go\nfunc ValidateEmail(email string) error\n```\nValidateEmail is the single point of logic for user email validations\n\n## Types\n\n```go\ntype ChangeAuthTokenRequest struct {\n\tPassword string `json:\"password\"`\n}\n```\nChangeauth.TokenRequest is sent when updating a User's authentication token.\n\n```go\ntype ORM interface {\n\tFindUser(email string) (User, error)\n\tFindUserByAPIToken(apiToken string) (User, error)\n\tListUsers() ([]User, error)\n\tAuthorizedUserWithSession(sessionID string) (User, error)\n\tDeleteUser(email string) error\n\tDeleteUserSession(sessionID string) error\n\tCreateSession(sr SessionRequest) (string, error)\n\tClearNonCurrentSessions(sessionID string) error\n\tCreateUser(user *User) error\n\tUpdateRole(email, newRole string) (User, error)\n\tSetAuthToken(user *User, token *auth.Token) error\n\tCreateAndSetAuthToken(user *User) (*auth.Token, error)\n\tDeleteAuthToken(user *User) error\n\tSetPassword(user *User, newPassword string) error\n\tSessions(offset, limit int) ([]Session, error)\n\tGetUserWebAuthn(email string) ([]WebAuthn, error)\n\tSaveWebAuthn(token *WebAuthn) error\n\n\tFindExternalInitiator(eia *auth.Token) (initiator *bridges.ExternalInitiator, err error)\n}\n```\n\n```go\nfunc NewORM(db *sqlx.DB, sd time.Duration, lggr logger.Logger, cfg pg.QConfig, auditLogger audit.AuditLogger) ORM\n```\n\n```go\ntype Session struct {\n\tID        string    `json:\"id\"`\n\tEmail     string    `json:\"email\"`\n\tLastUsed  time.Time `json:\"lastUsed\"`\n\tCreatedAt time.Time `json:\"createdAt\"`\n}\n```\nSession holds the unique id for the authenticated session.\n\n```go\nfunc NewSession() Session\n```\nNewSession returns a session instance with ID set to a random ID and LastUsed to now.\n\n```go\ntype SessionReaperConfig interface {\n\tSessionTimeout() models.Duration\n\tReaperExpiration() models.Duration\n}\n```\n\n```go\ntype SessionRequest struct {\n\tEmail          string `json:\"email\"`\n\tPassword       string `json:\"password\"`\n\tWebAuthnData   string `json:\"webauthndata\"`\n\tWebAuthnConfig WebAuthnConfiguration\n\tSessionStore   *WebAuthnSessionStore\n}\n```\nSessionRequest encapsulates the fields needed to generate a new SessionID, including the hashed password.\n\n```go\ntype User struct {\n\tEmail             string\n\tHashedPassword    string\n\tRole              UserRole\n\tCreatedAt         time.Time\n\tTokenKey          null.String\n\tTokenSalt         null.String\n\tTokenHashedSecret null.String\n\tUpdatedAt         time.Time\n}\n```\nUser holds the credentials for API user.\n\n```go\nfunc NewUser(email string, plainPwd string, role UserRole) (User, error)\n```\nNewUser creates a new user by hashing the passed plainPwd with bcrypt.\n\n```go\nfunc (u *User) GenerateAuthToken() (*auth.Token, error)\n```\nGenerateAuthToken randomly generates and sets the users Authentication Token.\n\n```go\nfunc (u *User) SetAuthToken(token *auth.Token) error\n```\nSetAuthToken updates the user to use the given Authentication Token.\n\n```go\ntype UserRole string\n```\n\n```go\nconst (\n\tUserRoleAdmin UserRole = \"admin\"\n\tUserRoleEdit  UserRole = \"edit\"\n\tUserRoleRun   UserRole = \"run\"\n\tUserRoleView  UserRole = \"view\"\n)\n```\n\n```go\nfunc GetUserRole(role string) (UserRole, error)\n```\nGetUserRole is the single point of logic for mapping role string to UserRole\n\n```go\ntype WebAuthn struct {\n\tEmail         string\n\tPublicKeyData sqlxTypes.JSONText\n}\n```\nWebAuthn holds the credentials for API user.\n\n```go\ntype WebAuthnConfiguration struct {\n\tRPID     string\n\tRPOrigin string\n}\n```\n\n```go\ntype WebAuthnSessionStore struct {\n\t// contains filtered or unexported fields\n}\n```\nWebAuthnSessionStore is a wrapper around an in memory key value store which provides some helper methods related to webauthn operations.\n\n```go\nfunc NewWebAuthnSessionStore() *WebAuthnSessionStore\n```\nNewWebAuthnSessionStore returns a new session store.\n\n```go\nfunc (store *WebAuthnSessionStore) BeginWebAuthnRegistration(user User, uwas []WebAuthn, config WebAuthnConfiguration) (*protocol.CredentialCreation, error)\n```\n\n```go\nfunc (store *WebAuthnSessionStore) FinishWebAuthnRegistration(user User, uwas []WebAuthn, response *http.Request, config WebAuthnConfiguration) (*webauthn.Credential, error)\n```\n\n```go\nfunc (store *WebAuthnSessionStore) GetWebauthnSession(key string) (data webauthn.SessionData, err error)\n```\nGetWebauthnSession unmarshals and returns the webauthn session information from the session cookie, which is removed.\n\n```go\nfunc (store *WebAuthnSessionStore) SaveWebauthnSession(key string, data *webauthn.SessionData) error\n```\nSaveWebauthnSession marshals and saves the webauthn data to the provided key given the request and responsewriter\n\n```go\ntype WebAuthnUser struct {\n\tEmail         string\n\tWACredentials []webauthn.Credential\n}\n```\nWebAuthnUser implements the required duo-labs/webauthn/ 'User' interface kept separate from our internal 'User' struct\n\n```go\nfunc (u WebAuthnUser) CredentialExcludeList() []protocol.CredentialDescriptor\n```\nCredentialExcludeList returns a CredentialDescriptor array filled with all the user's credentials to prevent them from re-registering keys\n\n```go\nfunc (u *WebAuthnUser) LoadWebAuthnCredentials(uwas []WebAuthn) error\n```\n\n```go\nfunc (u WebAuthnUser) WebAuthnCredentials() []webauthn.Credential\n```\nWebAuthnCredentials returns credentials owned by the user\n\n```go\nfunc (u WebAuthnUser) WebAuthnDisplayName() string\n```\nWebAuthnDisplayName returns the user's display name. In this case we just return the email\n\n```go\nfunc (u WebAuthnUser) WebAuthnID() []byte\n```\nWebAuthnID returns the user's ID\n\n```go\nfunc (u WebAuthnUser) WebAuthnIcon() string\n```\nWebAuthnIcon should be the logo in some form. How it should be is currently unclear to me.\n\n```go\nfunc (u WebAuthnUser) WebAuthnName() string\n```\nWebAuthnName returns the user's email\n\n","path":"github.com/GoPlugin/pluginV2/core/sessions","name":"sessions","synopsis":"","isRedistributable":true}
