cwtch/protocol/connections/engine_token_handler.go

47 lines
1.9 KiB
Go
Raw Normal View History

2022-09-06 19:13:32 +00:00
package connections
import (
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/protocol/groups"
"git.openprivacy.ca/cwtch.im/tapir/primitives/privacypass"
"strconv"
)
// Implement Token Service Handler for Engine
// GroupMessageHandler receives a server and an encrypted group message
func (e *engine) GroupMessageHandler(server string, gm *groups.EncryptedGroupMessage) {
e.receiveGroupMessage(server, gm)
}
// ServerAuthedHandler is notified when a server has successfully authed
func (e *engine) ServerAuthedHandler(server string) {
e.serverAuthed(server)
}
// ServerSyncedHandler is notified when a server has successfully synced
func (e *engine) ServerSyncedHandler(server string) {
e.serverSynced(server)
}
// ServerClosedHandler is notified when a server connection has closed, the result is ignored during shutdown...
func (e *engine) ServerClosedHandler(server string) {
e.ignoreOnShutdown(e.serverDisconnected)(server)
}
// NewTokenHandler is notified after a successful token acquisition
func (e *engine) NewTokenHandler(tokenService string, tokens []*privacypass.Token) {
tokenManagerPointer, _ := e.tokenManagers.LoadOrStore(tokenService, new(TokenManager))
tokenManager := tokenManagerPointer.(*TokenManager)
tokenManager.NewTokens(tokens)
e.eventManager.Publish(event.NewEvent(event.TokenManagerInfo, map[event.Field]string{event.ServerTokenOnion: tokenService, event.ServerTokenCount: strconv.Itoa(tokenManager.NumTokens())}))
}
// FetchToken is notified when a server requires a new token from the client
func (e *engine) FetchToken(tokenService string) (*privacypass.Token, int, error) {
tokenManagerPointer, _ := e.tokenManagers.LoadOrStore(tokenService, new(TokenManager))
tokenManager := tokenManagerPointer.(*TokenManager)
e.eventManager.Publish(event.NewEvent(event.TokenManagerInfo, map[event.Field]string{event.ServerTokenOnion: tokenService, event.ServerTokenCount: strconv.Itoa(tokenManager.NumTokens())}))
return tokenManager.FetchToken()
}