auth

package
v0.2.2-post.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 15 Imported by: 0

README

auth

import "github.com/forestvpn/cli/auth"

auth is a package containing an authentication client built around Firebase REST API.

See https://firebase.google.com/docs/reference/rest for more information.

fields is a package containing structures for storing user prompted data such as email or password.

forms is package contaning structures to work with user input during Firebase authentication process.

Authentication related utilities around firebase REST authentication workflow. See https://firebase.google.com/docs/reference/rest for more information.

Index

Variables

AppDir is Forest CLI application directory.

var AppDir = home + "/.forestvpn/"

Client is a REST client for Go.

See https://github.com/go-resty/resty for more information.

var Client = resty.New()

The DeviceFile represents the device created for the user.

Read more: https://github.com/forestvpn/api-client-go.

var DeviceFile = AppDir + "device.json"

FirebaseAuthFile is a file to dump Firebase responses.

var FirebaseAuthFile = AppDir + "firebase.json"

The SessionFile is a file for storing the last session information.

It's used to track down the status of connection.

var SessionFile = AppDir + "session.json"

WireguardConfig is a Wireguard configuration file.

It's being rewrittten per location change.

var WireguardConfig = AppDir + "fvpn0.conf"

func BuyPremiumDialog

func BuyPremiumDialog() error

BuyPremiumDialog is a function that prompts the user to by premium subscrition on Forest VPN. If the user prompts 'yes', then it opens https://forestvpn.com/pricing/ page in the default browser.

func HandleFirebaseAuthResponse

func HandleFirebaseAuthResponse(response *resty.Response) error

HandleFirebaseAuthResponse is a function to extracts the error message from the Firebase REST API response when the status is ok.

func HandleFirebaseSignInResponse

func HandleFirebaseSignInResponse(response *resty.Response) error

HandleFirebaseSignInResponse is a function that dumps the Firebase REST API response into FirebaseAuthFile.

func Init

func Init() error

Init is a function that creates directories structure for Forest CLI.

func IsAuthenticated

func IsAuthenticated() bool

IsAuthenticated is a helper function to quickly check wether user is authenticated by checking existance of an access token.

func IsDeviceCreated

func IsDeviceCreated() bool

Deprecated: IsDeviceCreated is a function that checks if the DeviceFile exist, i.e device is created.

func IsLocationSet

func IsLocationSet() bool

IsLocationSet is a function to check wether Wireguard configuration file created after location selection.

func IsRefreshTokenExists

func IsRefreshTokenExists() bool

IsRefreshTokenExists is a function to quickly check refresh token exists locally.

func JsonDump

func JsonDump(data []byte, filepath string) error

JsonDump is a function that dumps the json data into the file at filepath.

func JsonLoad

func JsonLoad(filepath string) (map[string]string, error)

JsonLoad is a function that reads the content of a file and unmarshals it into the map. If there's no file or it is empty, returns an empty map.

func LoadAccessToken

func LoadAccessToken() (string, error)

LoadAccessToken is a function to quickly get the local access token from FirebaseAuthFile.

func LoadDeviceID

func LoadDeviceID() (string, error)

LoadDeviceID is a function to quickly get the device ID from the DeviceFile.

func LoadRefreshToken

func LoadRefreshToken() (string, error)

LoadRefreshToken is a function to quickly get the local refresh token from FirebaseAuthFile.

type AuthClient

AuthClient is a structure used as a Firebase REST client.

See https://firebase.google.com/docs/reference/rest for more information.

type AuthClient struct {
    ApiKey string
}
func (AuthClient) ExchangeRefreshForIdToken
func (c AuthClient) ExchangeRefreshForIdToken(refreshToken string) (*resty.Response, error)

See https://firebase.google.com/docs/reference/rest/auth#section-refresh-token for more information.

func (AuthClient) GetAccessToken
func (c AuthClient) GetAccessToken() (*resty.Response, error)

GetAccessToken is a method to obtain a new access token from Firebase REST API and dump the response into FirebaseAuthFile.

func (AuthClient) GetUserData
func (c AuthClient) GetUserData(email string) (*resty.Response, error)

GetUserInfo is used to check if the user already exists in the Firebase database during the registration.

See https://firebase.google.com/docs/reference/rest/auth#section-get-account-info for more information.

func (AuthClient) SignIn
func (c AuthClient) SignIn(form SignInForm) (*resty.Response, error)

SignIn is a method to perform a Firebase sign in request. It accepts an instance of a SignInForm that holds validated data for request.

See https://firebase.google.com/docs/reference/rest/auth#section-sign-in-email-password for more information.

func (AuthClient) SignUp
func (c AuthClient) SignUp(form SignUpForm) (*resty.Response, error)

SignUp is a method to perform a Firebase sign up request. It accepts an instance of a SignUpForm that holds validated data for request.

See https://firebase.google.com/docs/reference/rest/auth#section-create-email-password for more information.

type EmailField

EmailField is a structure that holds an email value that could be validated.

type EmailField struct {
    Value string
}
func GetEmailField
func GetEmailField(email string) (EmailField, error)

GetEmailField is a method that prompts a user an email and then validates it.

func (EmailField) Validate
func (f EmailField) Validate() error

Validate is a method to check user's email address.

type Info

type Info struct {
    AdditionalProperties string
}

type InfoForm

type InfoForm struct {
    Type string
    Info Info
}

type PasswordConfirmationField

PasswordConfirmationField is used in the SignUpForm to store the confirmation of a password.

type PasswordConfirmationField struct {
    Value []byte
}

type PasswordField

PasswordField is a structure that holds a password value that could be validated.

type PasswordField struct {
    Value []byte
}
func GetPasswordField
func GetPasswordField(password []byte) (PasswordField, error)

getPasswordField is a method that prompts the user a password and then validates it.

func (PasswordField) Validate
func (p PasswordField) Validate() error

Validate is a method to check password's strength.

type SignInForm

SignInForm is a structure to store user's email and password.

type SignInForm struct {
    EmailField
    PasswordField
}

type SignUpForm

SignUpForm is a structure that holds the SignInForm and the password confirmation.

type SignUpForm struct {
    SignInForm
    PasswordConfirmationField
}
func (SignUpForm) ValidatePasswordConfirmation
func (s SignUpForm) ValidatePasswordConfirmation() error

ValidatePasswordConfirmation is a method that compares password and the password confirmation values.

Generated by gomarkdoc

Documentation

Overview

auth is a package containing an authentication client built around Firebase REST API.

See https://firebase.google.com/docs/reference/rest for more information.

fields is a package containing structures for storing user prompted data such as email or password.

forms is package contaning structures to work with user input during Firebase authentication process.

Authentication related utilities around firebase REST authentication workflow. See https://firebase.google.com/docs/reference/rest for more information.

Index

Constants

View Source
const AccountsMapFile = ".accounts.json"

AccountsMapFile is a file to dump the AccountsMap structure that holds the mappings from user logged in emails to uuids.

View Source
const ActiveUserLockFile string = "/.active.lock"
View Source
const BillingFeatureFile = "/billing.json"

BillingFeatureFile is a file to store user's billing features locally.

View Source
const DeviceFile string = "/device.json"

The DeviceFile represents the device created for the user.

Read more: https://github.com/forestvpn/api-client-go.

View Source
const FirebaseAuthFile = "/firebase.json"

FirebaseAuthFile is a file to dump Firebase responses.

View Source
const FirebaseExtensionFile = "/firebase-ext.json"

FirebaseExtensionFile contains access token expiration date recieved from firebase login response's expire time value.

View Source
const WireguardConfig = "/fvpn0.conf"

WireguardConfig is a Wireguard configuration file.

It's being rewrittten per location change.

Variables

View Source
var AppDir = home + "/.forestvpn/"

AppDir is Forest CLI application directory.

View Source
var ProfilesDir = AppDir + "profiles/"
View Source
var SessionFile = AppDir + "session.json"

Deprecated: The SessionFile is a file for storing the last session information.

It's used to track down the status of connection.

Functions

func BillingFeatureExpired

func BillingFeatureExpired(billingFeature forestvpn_api.BillingFeature) bool

BillingFeatureExpired is a function to check whether the expire date of given billing feature is before current local time.

func BillingFeautureExists

func BillingFeautureExists(userID string) bool

BillingFeautureExists is a helper function to quickly check whether the local billing features exists for the user with id value of given user id.

func Date2Json

func Date2Json(date time.Time) ([]byte, error)

Date2Json is a function that marshals time.Time type into json string.

func DumpAccessTokenExpireDate

func DumpAccessTokenExpireDate(userID string, expires_in string) error

DumpAccessTokenExpireDate is a function to write access token exparation date into the FirebaseExtensionFile for the user with id value of given user id.

func GetAccessTokenExpireDate

func GetAccessTokenExpireDate(expireTime string) (time.Time, error)

GetAccessTokenExpireDate is a function to create a time.Time object from numeric string value. The numeric string values is a value that comes from the firebase login response and indicates the duration of an access token exparation period in seconds. The numeric string value is added to the current local time right after the firebase login response. Thus, GetAccessTokenExpireDate returns the time object with exparation date of an access token.

func HandleFirebaseAuthResponse

func HandleFirebaseAuthResponse(response *resty.Response) (string, error)

HandleFirebaseAuthResponse is a function to extracts the error message from the Firebase REST API response when the status is ok.

func HandleFirebaseSignInResponse

func HandleFirebaseSignInResponse(response *resty.Response) error

HandleFirebaseSignInResponse is a function that dumps the Firebase REST API response into FirebaseAuthFile.

func HandleFirebaseSignUpResponse

func HandleFirebaseSignUpResponse(response *resty.Response) error

HandleFirebaseSignUpResponse is a function to check whether there's an error in firebase response from it's sign up endpoint.

See https://firebase.google.com/docs/reference/rest/auth#section-create-email-password for more information.

func Init

func Init() error

Init is a function that creates directories structure for Forest CLI.

func IsAccessTokenExpired

func IsAccessTokenExpired(userID string) (bool, error)

IsAccessTokenExpired is a function that reads an access token exparation date of the user with id value of given user id from the local firebase sign in response.

func IsActiveProfile

func IsActiveProfile(userID string) bool

IsActiveProfile is a function to check if the given user ID is an ID of currently active (logged in) user.

func IsAuthenticated

func IsAuthenticated() bool

IsAuthenticated is a helper function to quickly check wether user is authenticated by checking existance of an access token.

func IsRefreshTokenExists

func IsRefreshTokenExists() (bool, error)

IsRefreshTokenExists is a function to quickly check refresh token exists locally.

func JsonDump

func JsonDump(data []byte, filepath string) error

JsonDump is a function that dumps the json data into the file at filepath.

func LoadAccessToken

func LoadAccessToken(userID string) (string, error)

LoadAccessToken is a helper function to quickly read and return access token of specific user by user ID from the firebase login response avalable locally.

func LoadAccessTokenExpireDate

func LoadAccessTokenExpireDate(userID string) (time.Time, error)

LoadAccessTokenExpireDate is a function to load the exparation date of an access token for user with id value of given user id.

func LoadBillingFeatures

func LoadBillingFeatures(userID string) ([]forestvpn_api.BillingFeature, error)

LoadBillingFeatures is a function to read local billing features from file for the user with id value of given user id.

func LoadDevice

func LoadDevice(userID string) (*forestvpn_api.Device, error)

LoadDevice is a function that reads local device file depending on the user ID provided and returns it as a forestvpn_api.Device.

func LoadFirebaseAuthFile

func LoadFirebaseAuthFile(userID string) (map[string]any, error)

LoadFirebaseAuthFile is a function to get local firebase authentication response per user by the user ID.

func LoadFirebaseExtensionFile

func LoadFirebaseExtensionFile(userID string) (map[string]string, error)

LoadFirebaseExtensionFile is a function to read FirebaseExtensionFile and return an access token expire date it contains for the user with id value of given user id.

func LoadIdToken

func LoadIdToken(userID string) (string, error)

LoadIdToken is a function to load id token of the user with id value of given user id from the local firebase login response.

func LoadRefreshToken

func LoadRefreshToken() (string, error)

LoadRefreshToken is a function to quickly get the local refresh token from FirebaseAuthFile.

func LoadUserID

func LoadUserID() (string, error)

LoadUserID is a helper function to quickly read and return user ID of currently active user from the firebase login response avalable locally.

func RemoveActiveUserLockFile

func RemoveActiveUserLockFile() error

RemoveActiveUserLockFile is a helper function to quickly diactivate currently active (logged in) user.

func RemoveFirebaseAuthFile

func RemoveFirebaseAuthFile(userID string) error

RemoveFirebaseAuthFile is a function to remove FirebaseAuthFile of the user with given user ID. Used to log out the user.

func SetActiveProfile

func SetActiveProfile(userID string) error

SetActiveProfile is a function to set the user profile with given user ID active. To set the profile active: 1. Find another active profile if exists 2. Diactivate currently active profile if found on step 1. 3. Activate profile with id of given userID

How the profiles are activated or diactivated.

Each user profile isolated in the own profile directory named by user's uuid. When profile X is activated: 1. The lock file is removed from other profiles directories if found. 2. The lock file is created in the directory of X profile.

ActiveUserLockFile - is a lock file. It's an empty file that only serves to indicate whether profile is active. When profile X is diactivated, the lock file is removed from the X profile directory.

func UpdateProfileDevice

func UpdateProfileDevice(device *forestvpn_api.Device) error

UpdateProfileDevice is a helper function to quickly update the local device file of the logged in (active) user.

Types

type AccountsMap

type AccountsMap struct {
	// contains filtered or unexported fields
}

AccountsMap is a structure that helps to track down user's logins and logouts. It also helps to show local accounts in account ls command.

func GetAccountsMap

func GetAccountsMap(accountsMapFile string) AccountsMap

GetAccountMap is a faactory function to get the AccountsMap structure

func (AccountsMap) AddAccount

func (a AccountsMap) AddAccount(email string, userID string) error

AddAccount is a method to add a new logged in account to the AccountMap.

func (AccountsMap) GetEmail

func (a AccountsMap) GetEmail(userID string) string

func (AccountsMap) GetUserID

func (a AccountsMap) GetUserID(email string) string

GetUserID is a method to get uuid of a user by related email.

func (AccountsMap) ListLocalAccounts

func (a AccountsMap) ListLocalAccounts() error

ListLocalAccounts is a method to print local user accounts in a table.

func (AccountsMap) RemoveAccount

func (a AccountsMap) RemoveAccount(userID string) error

RemoveAccount is a method to remove knonw local account from AccountMap. It loads AccountMap structure from the AccountMapFile, removes the key with value of userID and dumps updated data using JsonDump.

type AuthClient

type AuthClient struct {
	ApiKey string
}

AuthClient is a structure used as a Firebase REST client.

See https://firebase.google.com/docs/reference/rest for more information.

func (AuthClient) GetAccessToken

func (c AuthClient) GetAccessToken(refreshToken string) (*resty.Response, error)

GetAccessToken is a method to obtain a new access token from Firebase REST API and dump the response into FirebaseAuthFile.

func (AuthClient) GetUserData

func (c AuthClient) GetUserData(idToken string) (*resty.Response, error)

GetUserData is used to check if the user already exists in the Firebase database during the registration.

See https://firebase.google.com/docs/reference/rest/auth#section-get-account-info for more information.

func (AuthClient) SignIn

func (c AuthClient) SignIn(form SignInForm) (*resty.Response, error)

SignIn is a method to perform a Firebase sign in request. It accepts an instance of a SignInForm that holds validated data for request.

See https://firebase.google.com/docs/reference/rest/auth#section-sign-in-email-password for more information.

func (AuthClient) SignUp

func (c AuthClient) SignUp(form SignUpForm) (*resty.Response, error)

SignUp is a method to perform a Firebase sign up request. It accepts an instance of a SignUpForm that holds validated data for request.

See https://firebase.google.com/docs/reference/rest/auth#section-create-email-password for more information.

type EmailField

type EmailField struct {
	Value string
}

EmailField is a structure that holds an email value that could be validated.

func GetEmailField

func GetEmailField(email string) (EmailField, error)

GetEmailField is a method that prompts a user an email and then validates it.

func (EmailField) Validate

func (f EmailField) Validate() error

Validate is a method to check user's email address.

type Info

type Info struct {
	AdditionalProperties string
}

type InfoForm

type InfoForm struct {
	Type string
	Info Info
}

type PasswordConfirmationField

type PasswordConfirmationField struct {
	Value []byte
}

PasswordConfirmationField is used in the SignUpForm to store the confirmation of a password.

type PasswordField

type PasswordField struct {
	Value []byte
}

PasswordField is a structure that holds a password value that could be validated.

func GetPasswordField

func GetPasswordField(password []byte, validate bool) (PasswordField, error)

getPasswordField is a method that prompts the user a password and then validates it. validate is a boolean that allows to enable or disable password validation. E.g. when password validation is needed on registration but not on login.

func (PasswordField) Validate

func (p PasswordField) Validate() error

Validate is a method to check password's strength.

type SignInForm

type SignInForm struct {
	EmailField
	PasswordField
}

SignInForm is a structure to store user's email and password.

type SignUpForm

type SignUpForm struct {
	SignInForm
	PasswordConfirmationField
}

SignUpForm is a structure that holds the SignInForm and the password confirmation.

func (SignUpForm) ValidatePasswordConfirmation

func (s SignUpForm) ValidatePasswordConfirmation() error

ValidatePasswordConfirmation is a method that compares password and the password confirmation values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL