Documentation
¶
Index ¶
- Variables
- type App
- func (app *App) Attributes() []string
- func (app *App) Bytes() []byte
- func (app *App) ID(secret *Secret) *AppID
- func (app *App) Marshal() []byte
- func (app *App) SetAttributes(attrs []string) *App
- func (app *App) SetBytes(data []byte) *App
- func (app *App) SetID(id *AppID) *App
- func (app *App) SetSecret(secret *Secret) *App
- func (app *App) SetString(data string) *App
- func (app *App) String() string
- func (app *App) Unmarshal(data []byte) *App
- func (app *App) Valid(secretHash []byte) bool
- type AppID
- func (id *AppID) Bytes() []byte
- func (id *AppID) GenerateToken(secret Secret, email string) Token
- func (id *AppID) Message(email string, exp Expiration) []byte
- func (id *AppID) PrivKey(secret Secret) ed25519.PrivateKey
- func (id *AppID) SetBytes(data []byte) *AppID
- func (id *AppID) SetString(data string) *AppID
- func (id *AppID) Sign(secret Secret, msg []byte) []byte
- func (id *AppID) String() string
- func (id *AppID) Verify(secret Secret, msg, sig []byte) bool
- func (id *AppID) VerifyToken(token Token, secret Secret, email string) bool
- type Expiration
- func (exp *Expiration) Bytes() []byte
- func (exp *Expiration) Duration() time.Duration
- func (exp *Expiration) Marshal() []byte
- func (exp *Expiration) SetBytes(data []byte) *Expiration
- func (exp *Expiration) SetDuration(d time.Duration) *Expiration
- func (exp *Expiration) SetString(data string) *Expiration
- func (exp *Expiration) SetTime(t time.Time) *Expiration
- func (exp *Expiration) String() string
- func (exp *Expiration) Time() time.Time
- func (exp *Expiration) Unmarshal(data []byte) *Expiration
- func (exp *Expiration) Valid() bool
- type Secret
- type Token
- func (t *Token) Bytes() []byte
- func (t *Token) Expiration() *Expiration
- func (t *Token) SetBytes(data []byte) *Token
- func (t *Token) SetExpiration(exp Expiration) *Token
- func (t *Token) SetSignature(sig []byte) *Token
- func (t *Token) SetString(data string) *Token
- func (t *Token) Signature() []byte
- func (t *Token) String() string
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type App ¶
type App struct {
Name string
RedirectURI string
SessionDuration time.Duration
AppSecretHash []byte
}
App represents an application that can request tokens. It has a name, a redirect URI, and a session duration.
func (*App) Attributes ¶
Attributes method returns the app's attributes as a slice of strings. This is useful for encoding the app.
func (*App) Bytes ¶
Bytes method returns the app as a byte slice. This is useful for encoding the app. It is equivalent to converting the app to a string and then converting the string to a byte slice.
func (*App) ID ¶
ID method returns the app ID of the app. The app ID is a self-contained representation of the app that can be used to generate tokens. It is created by encoding the app as a base64-encoded byte slice using the Marshal method.
func (*App) Marshal ¶
Marshal method returns the app as a base64-encoded byte slice. It is used to be included in the app ID, which makes it self-contained.
func (*App) SetAttributes ¶
SetAttributes method sets the app's attributes from a slice of strings. This is useful for decoding the app.
func (*App) SetBytes ¶
SetBytes method sets the app from a byte slice. This is useful for decoding the app. It is equivalent to converting the byte slice to a string and then converting the string to the app.
func (*App) SetID ¶
SetID method sets the app from an app ID. The app ID is a self-contained representation of the app that can be used to generate tokens. The app is extracted from the app ID by decoding the app as a base64-encoded byte slice using the Unmarshal method.
func (*App) SetString ¶
SetString method sets the app from a string. This is useful for decoding the app. The string should be the app's attributes joined by the app data separator.
func (*App) String ¶
String method returns the app as a string. This is useful for debugging and encoding the app. The resulting string is the app's attributes joined by the app data separator.
type AppID ¶
type AppID []byte
AppID represents an application ID that is used to generate and verify tokens. It is a wrapper around a byte slice that provides additional methods for setting and getting the application ID.
func (*AppID) GenerateToken ¶
GenerateToken method returns a token for the application ID. If the app ID is nil, the secret is invalid, or the email is empty, nil is returned. It internally calls the Message method to generate the message for the token and uses it to sign the message. The token is generated by hashing the application ID with the email and expiration time, and signing the result with the secret. The signature is then used to create a token with the expiration time and signature.
func (*AppID) Message ¶
func (id *AppID) Message(email string, exp Expiration) []byte
Message method returns the message for the application ID. If the app ID is nil, the email is empty, or the expiration time is invalid, nil is returned. It is used to generate the message for the token by hashing the application ID with the email and expiration time. The message is generated by appending the application ID with the email and expiration time, and hashing the result with sha256 to create a unique message for the token.
func (*AppID) PrivKey ¶
func (id *AppID) PrivKey(secret Secret) ed25519.PrivateKey
PrivKey method returns the private key for the application ID. If the application ID is nil or the secret is invalid, nil is returned. It internally calls the Bytes method to get the application ID as a byte slice and uses it to generate the private key. The private key is generated by hashing the application ID with the secret and using the resulting hash as the seed for an ed25519 private key.
func (*AppID) SetBytes ¶
SetBytes method sets the application ID from a byte slice. If the application ID is nil, a new application ID is created. If the byte slice is empty, the application ID is not set. It internally calls the Unmarshal method of the App to check that the application ID is valid before setting the application ID from the byte slice. If the resulting application is not valid, the application ID is not set and nil is returned.
func (*AppID) SetString ¶
SetString method sets the application ID from a string. If the application ID is nil, a new application ID is created. If the string is empty, the application ID is not set. It internally calls the SetBytes method to set the application ID from a byte slice.
func (*AppID) Sign ¶
Sign method returns the signature of the message for the application ID. If the application ID is nil, the message is empty, or the secret is invalid, nil is returned. It internally calls the PrivKey method to get the private key for the application ID and uses it to sign the message. The message is signed by appending a nonce to it and hashing the result with the private key. The signature is then encoded to base64 before being returned to be used as a part of the token, keeping it as short as possible.
func (*AppID) String ¶
String method returns the application ID as a string. If the application ID is nil, an empty string is returned. It internally calls the Bytes method to get the application ID as a byte slice and converts it to a string.
func (*AppID) Verify ¶
Verify method returns true if the signature of the message is valid for the application ID. If the application ID is nil, the message is empty, the signature is empty, or the secret is invalid, false is returned. It internally calls the PrivKey method to get the private key for the application ID and uses it to verify the signature. The message is verified by appending a nonce to it and hashing the result with the public key. The signature is then decoded from base64 and verified with the public key to ensure that it was signed by the private key.
func (*AppID) VerifyToken ¶
VerifyToken method returns true if the token is valid for the application ID. If the app ID is nil, the token is nil, the secret is invalid, or the email is empty, false is returned. It is used to verify the token by checking that the expiration time is valid and the signature is correct. The token is verified by hashing the application ID with the email and expiration time, and verifying the signature with the secret.
type Expiration ¶
Expiration represents a time when a token expires. It is a wrapper around time.Time that provides additional methods for setting and getting the expiration time.
func (*Expiration) Bytes ¶
func (exp *Expiration) Bytes() []byte
Bytes method returns the expiration time as a byte slice. It is useful for encoding the expiration time. If the expiration is nil, nil is returned. It is equivalent to converting the expiration time to a string and then converting the string to a byte slice.
func (*Expiration) Duration ¶
func (exp *Expiration) Duration() time.Duration
Duration method returns the duration until the expiration time.
func (*Expiration) Marshal ¶
func (exp *Expiration) Marshal() []byte
Marshal method returns the expiration time as a base64 encoded byte slice. It is useful for encoding the expiration time. If the expiration is nil or invalid, nil is returned.
func (*Expiration) SetBytes ¶
func (exp *Expiration) SetBytes(data []byte) *Expiration
SetBytes method sets the expiration time from a byte slice. It is useful for decoding the expiration time. It is equivalent to converting the byte slice to a string and then setting the expiration time from the string.
func (*Expiration) SetDuration ¶
func (exp *Expiration) SetDuration(d time.Duration) *Expiration
SetDuration method sets the expiration time from a duration. If the duration is invalid, the expiration time is not set.
func (*Expiration) SetString ¶
func (exp *Expiration) SetString(data string) *Expiration
SetString method sets the expiration time from a string in RFC3339Nano format. It is useful for decoding the expiration time. If the string is invalid, the expiration time is not set and nil is returned. If the expiration is nil, a new expiration is created. If the resulting expiration is invalid, nil is returned.
func (*Expiration) SetTime ¶
func (exp *Expiration) SetTime(t time.Time) *Expiration
SetTime method sets the expiration time from a time.Time. If the expiration is nil, a new expiration is created.
func (*Expiration) String ¶
func (exp *Expiration) String() string
String method returns the expiration time as a string in RFC3339Nano format. It is useful for encoding the expiration time. If the expiration is nil, an empty string is returned.
func (*Expiration) Time ¶
func (exp *Expiration) Time() time.Time
Time method returns the expiration time as a time.Time.
func (*Expiration) Unmarshal ¶
func (exp *Expiration) Unmarshal(data []byte) *Expiration
Unmarshal method sets the expiration time from a base64 encoded byte slice. It is useful for decoding the expiration time. If the expiration is nil or invalid, nil is returned.
func (*Expiration) Valid ¶
func (exp *Expiration) Valid() bool
Valid method returns true if the expiration is valid, false otherwise. An expiration is considered valid if it is in the future.
type Secret ¶
type Secret []byte
Secret represents a secret that is used to sign and verify tokens. It is a wrapper around a byte slice that provides additional methods for setting and getting the secret. It should have at least 2 parts, each hashed to a sha256 size.
func (*Secret) Hash ¶
Hash method returns the hash of the secret as a byte slice. The hash is created by hashing the secret to a sha256 size. The hash is used to create the app ID and it is part of it, but is also used to sign and verify the user sessions in the token generation process.
type Token ¶
type Token []byte
Token is a type that represents a user token. It is a wrapper around a byte slice that provides additional methods for setting and getting the token. It should have 2 parts, the first part is the expiration time, and the second part is the signature.
func (*Token) Bytes ¶
Bytes method returns the token as a byte slice. It is useful for encoding the token. If the token is nil, nil is returned. It internally calls the parts method to get the token's expiration time and signature as byte slices. It checks that the parts are valid before returning the token as a byte slice.
func (*Token) Expiration ¶
func (t *Token) Expiration() *Expiration
Expiration method returns the token's expiration time. It is useful for getting the expiration time. If the token is nil, nil is returned. It internally calls the parts method to get the token's expiration time and signature as byte slices. It checks that the expiration time is valid before returning it.
func (*Token) SetBytes ¶
SetBytes method sets the token from a byte slice. It is useful for decoding the token. The byte slice should be the token's expiration time and signature joined by the token separator. If the token is invalid, the token is not set. It internally calls the parts method to get the token's expiration time and signature as byte slices. It checks that the parts are valid before setting the token from the byte slice.
func (*Token) SetExpiration ¶
func (t *Token) SetExpiration(exp Expiration) *Token
SetExpiration method sets the token's expiration time. If the token is nil, a new token is created. If the expiration time is invalid, the token is not set. It internally calls the parts method to replace the token's expiration time with the new expiration time. It checks that the expiration time is valid before setting it.
func (*Token) SetSignature ¶
SetSignature method sets the token's signature. If the token is nil, a new token is created. If the signature is nil, the token is not set. It internally calls the parts method to replace the token's signature with the new signature.
func (*Token) SetString ¶
SetString method sets the token from a string. It is useful for decoding the token. The string should be the token's expiration time and signature joined by the token separator. If the token is invalid, the token is not set. It internally calls the SetBytes method to set the token from a byte slice.