Documentation ¶
Index ¶
- Variables
- func ReturnError(w http.ResponseWriter, status int, code, msg string)
- type ApiKey
- type App
- type ArchivedCrash
- type Backend
- func (b *Backend) AddCorsAddress(corsAddr string)
- func (b *Backend) AddUserAuth(userTable Table[User], privKey, pubKey []byte)
- func (b *Backend) EnableManagementKey(managementID string)
- func (b *Backend) GenerateJWT(r *ReqestUser) (string, error)
- func (b *Backend) GetApp(a *ApiKey) App
- func (b *Backend) HandleFunc(pattern string, h http.HandlerFunc)
- func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error)
- func (b *Backend) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (b *Backend) TryLogin(ctx context.Context, username, password string) (User, error)
- func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm string, ...) (*ParsedHeader, error)
- func (b *Backend) VerifyUser(ctx context.Context, token string) (*User, error)
- type CallbackApp
- type CountLog
- type CountTable
- type CrashFilterApp
- type CrashReport
- type CrashTable
- type ExtendedApp
- type IDStruct
- type IndividualCrash
- type ParsedHeader
- type ReqestUser
- type Table
- type User
Constants ¶
This section is empty.
Variables ¶
var ()
var ( ErrLoginTimeout = errors.New("user is timed out") ErrLoginIncorrect = errors.New("username or password is incorrect") )
var (
ErrNotFound = errors.New("no matches found in table")
)
var (
ErrPasswordLength = errors.New("password length must be 12-128")
)
Functions ¶
func ReturnError ¶
func ReturnError(w http.ResponseWriter, status int, code, msg string)
Return an error response with the given status code, code, and message.
Types ¶
type ApiKey ¶
type App ¶
type App interface { AppID() string CountTable() CountTable CrashTable() CrashTable }
An application interface. Both LogTable and CrashTable are optional, if they return nil then requests will be forbidden.
func NewSimpleApp ¶
func NewSimpleApp(appID string, countTable CountTable, crashTable CrashTable) App
type ArchivedCrash ¶
type Backend ¶
type Backend struct {
// contains filtered or unexported fields
}
A simple backend that handles user authentication, user count, and crash reports.
func NewBackend ¶
Create a new Backend with the given apps. keyTable must be specified.
func (*Backend) AddCorsAddress ¶
Enable CORS for with the given cors address
func (*Backend) AddUserAuth ¶
Enables user creation and authentication.
func (*Backend) EnableManagementKey ¶
Enables the use of a management API key for crash and count.
func (*Backend) GenerateJWT ¶
func (b *Backend) GenerateJWT(r *ReqestUser) (string, error)
func (*Backend) GetApp ¶
Try to get the App associated with the given ApiKey. Returns nil if not found.
func (*Backend) HandleFunc ¶
func (b *Backend) HandleFunc(pattern string, h http.HandlerFunc)
Add values to the Backend's underlying ServeMux
func (*Backend) ParseHeader ¶
func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error)
Parses the X-API-Key and Authorization headers. If the API Key provided but invalid (either due to expiring or isn't found), ErrApiKeyUnauthorized is returned. If the Authorization header is present but invalid, ErrTokenUnauthorized is returned. NOTE: An invalid apiKey will cause a nil return, but a invalid token will not. Token parsing is only
func (*Backend) ServeHTTP ¶
func (b *Backend) ServeHTTP(w http.ResponseWriter, r *http.Request)
http.Handler
func (*Backend) TryLogin ¶
Tries to login with the given username and password. If the user exists, but is timed out, the user is still returned.
func (*Backend) VerifyHeader ¶
func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm string, allowManagementKey bool) (*ParsedHeader, error)
Similiar to ParseHeader, but with key checking and automatic error returns. Guarentess Backend.GetApp is non-nil Checks that the key is a management key (not management permission and if allowManagement is true) or that it has the necessary permission. If the check if failed, ReturnError will be called and the returned *ParsedHeader will be nil. If token is present but invalid, no error will be returned just ParsedHeader.User will be nil. The error return will only be populated on "internal" errors and should *probably* be logged.
This function does not check the Key's appID so after calling VerifyHeader it's recommended to check the Key's appID.
type CallbackApp ¶
Provides an App access to it's parent *Backend. This is called only once, while setting up the Backend.
type CountLog ¶
type CountTable ¶
type CountTable interface { Table[CountLog] // Remove all Log items that have a CountLog.Date value less then the given value. RemoveOldLogs(ctx context.Context, date int) error // Get count. If platform is an empty string or "all", the full count should be given Count(ctx context.Context, platform string) (int, error) }
type CrashFilterApp ¶
type CrashFilterApp interface { App ShouldAddCrash(context.Context, IndividualCrash) bool }
Allows for an App to filter crashes before they get added to the DB, such as making sure the crash is from the correct version.
type CrashReport ¶
type CrashReport struct { ID string `json:"id" bson:"_id"` Error string `json:"error" bson:"error"` FirstLine string `json:"firstLine" bson:"firstLine"` Individual []IndividualCrash `json:"individual" bson:"individual"` }
func (CrashReport) GetID ¶
func (c CrashReport) GetID() string
type CrashTable ¶
type CrashTable interface { Table[CrashReport] // Move a crash type to archive. Crashes that match the archived crash will be automatically removed from the CrashTable. Archive(context.Context, ArchivedCrash) error IsArchived(context.Context, IndividualCrash) bool // Add the IndividualCrash report to the crash table. If a CrashReport exists that matches, then it gets added to CrashReport.Individual. // If an IndividualCrash exists that is a perfect match, Count is incremented instead of adding it to the array. InsertCrash(context.Context, IndividualCrash) error }
type ExtendedApp ¶
Allows an app more flexibility by directly interfacing with the backend's mux
type IndividualCrash ¶
type ParsedHeader ¶
type ParsedHeader struct { User *ReqestUser Key *ApiKey }
type Table ¶
type Table[T IDStruct] interface { Get(ctx context.Context, ID string) (data T, err error) Find(ctx context.Context, values map[string]any) ([]T, error) Insert(ctx context.Context, data T) error Remove(ctx context.Context, ID string) error FullUpdate(ctx context.Context, ID string, data T) error PartUpdate(ctx context.Context, ID string, update map[string]any) error }
type User ¶
type User struct { Perm map[string]string `json:"perm" bson:"perm"` ID string `json:"id" bson:"_id"` Username string `json:"username" bson:"username"` Password string `json:"password" bson:"password"` Salt string `json:"salt" bson:"salt"` Email string `json:"email" bson:"email"` Fails int `json:"fails" bson:"fails"` Timeout int64 `json:"timeout" bson:"timeout"` PasswordChange int64 `json:"passwordChange" bson:"passwordChange"` }
func (User) ToReqUser ¶
func (u User) ToReqUser() *ReqestUser