app

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAppDirPath

func GetAppDirPath() (string, error)

func StreamInterceptor

func StreamInterceptor(tokenManager *TokenManager, logger *logrus.Logger) grpc.StreamClientInterceptor

StreamInterceptor returns a gRPC stream interceptor that adds the authorization token to each stream request, except for the methods listed in publicMethods.

func UnaryInterceptor

func UnaryInterceptor(tokenManager *TokenManager, logger *logrus.Logger) grpc.UnaryClientInterceptor

UnaryInterceptor returns a gRPC interceptor that adds the authorization token to each request, except for the methods listed in AuthMethods.

Types

type AuthClient

type AuthClient struct {
	Client       auth.AuthServiceClient
	Logger       *logrus.Logger
	TokenManager *TokenManager
	AppDirPath   string
	SqliteStore  *store.SQLiteStore
	ParentClient *RpcClient // Reference to the parent RpcClient
}

AuthClient encapsulates the gRPC client and logger for authentication services.

func (*AuthClient) CreateLocalIdentityIfNewUserDevice

func (c *AuthClient) CreateLocalIdentityIfNewUserDevice(userID uint32) error

////////////////////////// Encryption key management//// CreateLocalIdentityIfNewUserDevice checks if the user-device is already registered, and if not, generates keys and uploads them.

func (*AuthClient) GetDeviceId

func (c *AuthClient) GetDeviceId() (uint32, error)

func (*AuthClient) GetPublicKeyBundle

func (c *AuthClient) GetPublicKeyBundle(userID, deviceID uint32) (*auth.PublicKeyBundleResponse, error)

func (*AuthClient) LoginUser

func (c *AuthClient) LoginUser(username, password string) (error, uint32)

LoginUser sends a login request to the server.

func (*AuthClient) LogoutUser

func (c *AuthClient) LogoutUser() error

LogoutUser gracefully logs out the user by canceling the context and closing the stream.

func (*AuthClient) PostLoginTasks

func (c *AuthClient) PostLoginTasks() error

PostLoginTasks opens the stream and starts listening for messages.

func (*AuthClient) RegisterUser

func (c *AuthClient) RegisterUser(username, password string) error

RegisterUser sends a registration request to the server.

type ChatClient

type ChatClient struct {
	Client           chat.ChatServiceClient                // gRPC client for chat service
	AuthClient       *AuthClient                           // Reference to AuthClient for authentication purposes
	Store            *store.SQLiteStore                    // Access to session and identity stores
	Logger           *logrus.Logger                        // Logger for logging messages and errors
	Stream           chat.ChatService_StreamMessagesClient // Persistent gRPC stream for sending messages
	ListenCancelFunc context.CancelFunc                    // Cancel function for stopping the message listener
	MessageChannel   chan *chat.MessageResponse            // Channel to send received messages
}

ChatClient encapsulates the gRPC client for chat services.

func (*ChatClient) DecryptMessage

func (cc *ChatClient) DecryptMessage(ctx context.Context, resp *chat.MessageResponse) (string, error)

func (*ChatClient) EncryptMessage

func (cc *ChatClient) EncryptMessage(ctx context.Context, recipientID, deviceID uint32, plaintext []byte) (message.Ciphertext, error)

SendMessage encrypts a message and sends it to the recipient through the chat service.

func (*ChatClient) InitializeSessionForRecipient

func (cc *ChatClient) InitializeSessionForRecipient(ctx context.Context, recipientID uint32) error

/////////////////////////////////////////////////////////////

func (*ChatClient) OpenPersistentStream

func (cc *ChatClient) OpenPersistentStream(ctx context.Context) error

OpenPersistentStream opens a persistent gRPC stream for sending and receiving messages.

func (*ChatClient) SendMessage

func (cc *ChatClient) SendMessage(ctx context.Context, recipientID, deviceID uint32, plaintext []byte) error

func (*ChatClient) SendUnencryptedMessage

func (cc *ChatClient) SendUnencryptedMessage(ctx context.Context, recipientID uint32, plaintext string) error

SendUnencryptedMessage sends a plaintext message to the recipient through the chat service without encryption.

type FriendsClient

type FriendsClient struct {
	Client friends.FriendManagementClient
	Logger *logrus.Logger
}

FriendsClient encapsulates the gRPC client for friend services.

func (*FriendsClient) AcceptFriendRequest

func (c *FriendsClient) AcceptFriendRequest(requestID int32) error

AcceptFriendRequest accepts an incoming friend request.

func (*FriendsClient) DeclineFriendRequest

func (c *FriendsClient) DeclineFriendRequest(requestID int32) error

DeclineFriendRequest declines an incoming friend request.

func (*FriendsClient) GetFriendList

func (c *FriendsClient) GetFriendList() ([]*friends.Friend, error)

GetFriendList retrieves the list of friends for the current user.

func (*FriendsClient) GetIncomingFriendRequests

func (c *FriendsClient) GetIncomingFriendRequests() ([]*friends.FriendRequest, error)

GetIncomingFriendRequests retrieves the incoming friend requests for the current user.

func (*FriendsClient) GetOutgoingFriendRequests

func (c *FriendsClient) GetOutgoingFriendRequests() ([]*friends.FriendRequest, error)

GetOutgoingFriendRequests retrieves the outgoing friend requests sent by the current user.

func (*FriendsClient) RemoveFriend

func (c *FriendsClient) RemoveFriend(friendID int32) error

RemoveFriend removes a friend from the user's friend list.

func (*FriendsClient) SendFriendRequest

func (c *FriendsClient) SendFriendRequest(recipientUsername string) error

SendFriendRequest sends a friend request to another user.

type RealTimeProvider

type RealTimeProvider struct{}

func (RealTimeProvider) Now

func (RealTimeProvider) Now() time.Time

type RpcClient

type RpcClient struct {
	AuthClient      *AuthClient
	FriendsClient   *FriendsClient
	ChatClient      *ChatClient
	Conn            *grpc.ClientConn
	Logger          *logrus.Logger
	AppDirPath      string
	Store           *store.SQLiteStore
	CurrentUserID   uint32
	CurrentDeviceID uint32
}

RpcClient manages multiple gRPC clients for different services.

func NewRpcClient

func NewRpcClient(config RpcClientConfig) (*RpcClient, error)

NewRpcClient initializes all service clients with a shared gRPC connection.

func (*RpcClient) CloseConnections

func (r *RpcClient) CloseConnections()

CloseConnections closes the shared gRPC connection.

func (*RpcClient) GetAppDirPath

func (r *RpcClient) GetAppDirPath() string

type RpcClientConfig

type RpcClientConfig struct {
	Conn          *grpc.ClientConn
	ServerAddress string
	Logger        *logrus.Logger
	AppDirPath    string
	TokenManager  *TokenManager
}

type TimeProvider

type TimeProvider interface {
	Now() time.Time
}

For testing

type TokenManager

type TokenManager struct {
	TimeProvider TimeProvider

	AuthClient *AuthClient // Reference to the parent AuthClient
	// contains filtered or unexported fields
}

TokenManager handles all operations related to managing tokens.

func NewTokenManager

func NewTokenManager(filePath string, client *AuthClient) *TokenManager

func (*TokenManager) GetAccessToken

func (tm *TokenManager) GetAccessToken() (string, error)

GetAccessToken returns a valid access token, refreshing it if necessary.

func (*TokenManager) GetClaimsFromAccessToken

func (tm *TokenManager) GetClaimsFromAccessToken() (map[string]interface{}, error)

GetClaimsFromAccessToken reads the stored access token and parses its JWT claims.

func (*TokenManager) GetUserIdFromAccessToken

func (tm *TokenManager) GetUserIdFromAccessToken() (uint32, error)

func (*TokenManager) IsTokenExpired

func (tm *TokenManager) IsTokenExpired(tokenString string) (bool, error)

Helper function to check if a token is expired by decoding the JWT payload.

func (*TokenManager) ReadTokens

func (tm *TokenManager) ReadTokens() (string, string, error)

ReadTokens retrieves the access and refresh tokens from the local file.

func (*TokenManager) RefreshAccessToken

func (tm *TokenManager) RefreshAccessToken(refreshToken string) (string, error)

RefreshAccessToken uses the refresh token to obtain a new access token from the server.

func (*TokenManager) SetClient

func (tm *TokenManager) SetClient(client *AuthClient)

SetClient allows updating the gRPC client.

func (*TokenManager) StoreTokens

func (tm *TokenManager) StoreTokens(accessToken, refreshToken string) error

StoreTokens stores the access and refresh tokens in a local file.

func (*TokenManager) TryAutoLogin

func (tm *TokenManager) TryAutoLogin() error

TryAutoLogin attempts to automatically log in the user using stored tokens. Actually, only refreshes the access token if it is expired. Otherwise, does nothing.

Jump to

Keyboard shortcuts

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