github.com/glide-im/glide@v1.6.0/pkg/gate/client.go (about) 1 package gate 2 3 import ( 4 "github.com/glide-im/glide/pkg/messages" 5 "strings" 6 ) 7 8 // tempIdPrefix is the prefix for temporary IDs in the second part of the ID. 9 const tempIdPrefix = "tmp@" 10 11 // idSeparator is the separator used to separate the part of the ID. 12 const idSeparator = "_" 13 14 // ID is used to identify the client, the ID is consist of multiple parts, some of them are optional. 15 // The ID is constructed by concatenating the parts with a '_' separator, and the parts are: 16 // - The gateway id (optional): the string id of the gateway that the client is connected to. 17 // - The client id (required): the string id of the client, it is unique for user. 18 // - if the client is temporary, this id is a string generated by the gateway and start with `tmp`. 19 // - The client type (optional): the int type of the client, like 'web', 'mobile', 'desktop', etc. 20 type ID string 21 22 // NewID2 creates a new ID from the given user id, use the empty gateway id and the empty client type. 23 func NewID2(uid string) ID { 24 return ID(strings.Join([]string{"", uid, ""}, idSeparator)) 25 } 26 27 // NewID creates a new ID from the given user id, gateway id and client type. 28 func NewID(gate string, uid string, device string) ID { 29 return ID(strings.Join([]string{gate, uid, device}, idSeparator)) 30 } 31 32 // Device returns the device type of the client, if the client device type is not set, it returns "". 33 func (i *ID) Device() string { 34 return i.getPart(2) 35 } 36 37 // UID returns the user id of the client, if the client is temporary, it returns "". 38 func (i *ID) UID() string { 39 return i.getPart(1) 40 } 41 42 // Gateway returns the gateway id of the client, if not set, it returns an empty string. 43 func (i *ID) Gateway() string { 44 return i.getPart(0) 45 } 46 47 // SetGateway sets the gateway part of the ID. 48 func (i *ID) SetGateway(gateway string) bool { 49 if strings.HasPrefix(string(*i), gateway) { 50 return false 51 } 52 s := strings.Split(string(*i), idSeparator) 53 if len(s) != 3 { 54 return false 55 } 56 s[0] = gateway 57 *i = ID(strings.Join(s, idSeparator)) 58 return true 59 } 60 61 // SetDevice sets the device type of the client. 62 func (i *ID) SetDevice(device string) bool { 63 if strings.HasSuffix(string(*i), device) { 64 return false 65 } 66 s := strings.Split(string(*i), idSeparator) 67 if len(s) != 3 { 68 return false 69 } 70 s[2] = device 71 *i = ID(strings.Join(s, idSeparator)) 72 return true 73 } 74 75 // IsTemp returns true if the ID is a temporary. 76 func (i *ID) IsTemp() bool { 77 return strings.HasPrefix(i.getPart(1), tempIdPrefix) 78 } 79 80 func (i *ID) Equals(other ID) bool { 81 return i.UID()+i.Device() == other.UID()+other.Device() 82 } 83 84 func (i *ID) getPart(index int) string { 85 s := strings.Split(string(*i), idSeparator) 86 if index >= len(s) { 87 return "" 88 } 89 return s[index] 90 } 91 92 // Info represents a client's information. 93 type Info struct { 94 95 // ID is the unique identifier for the client. 96 ID ID 97 98 // ConnectionId generated by client, used to identify the client connection. 99 ConnectionId string 100 101 // Version is the version of the client. 102 Version string 103 104 // AliveAt is the time the client was last seen. 105 AliveAt int64 106 107 // ConnectionAt is the time the client was connected. 108 ConnectionAt int64 109 110 // Gateway is the name of the gateway the client is connected to. 111 Gateway string 112 113 // CliAddr is the address of the client. 114 CliAddr string 115 } 116 117 // Client is a client connection abstraction. 118 type Client interface { 119 120 // SetID sets the ID of the client. 121 SetID(id ID) 122 123 // IsRunning returns true if the client is running/alive. 124 IsRunning() bool 125 126 // EnqueueMessage enqueues a message to be sent to the client. 127 EnqueueMessage(message *messages.GlideMessage) error 128 129 // Exit the client and close the connection. 130 Exit() 131 132 // Run starts the client message handling loop and blocks until the client. 133 Run() 134 135 // GetInfo returns the client's information. 136 GetInfo() Info 137 } 138 139 // ClientSecrets used to control client permission. 140 type ClientSecrets struct { 141 // MessageDeliverSecret is the secret of the client, used to authenticate the client message. 142 // The secret is generated by the business service, saved in business service, client should not know it. 143 // When client send a message to someone else, it should get the sign of the message target, and send it 144 // with the message. If business service want to control which one the client can send message to, 145 // business service can generate different secret for client, and notify the gateway update the secret, to make 146 // client old sign invalid. 147 MessageDeliverSecret string `json:"message_deliver_secret"` 148 } 149 150 // EncryptedCredential represents the encrypted credential. 151 type EncryptedCredential struct { 152 // Version is the version of the credential. 153 Version int `json:"version"` 154 155 // Credential is the encrypted credential string. 156 Credential string `json:"credential"` 157 } 158 159 // ClientAuthCredentials represents the client authentication credentials. 160 // Used to client authentication when connecting to the gateway, credentials are generated by business service, 161 // encrypted use the gateway's secret key, and sent to the client. 162 type ClientAuthCredentials struct { 163 164 // Type is the type of the client. 165 Type int `json:"type"` 166 167 // UserID uniquely identifies the client. 168 UserID string `json:"user_id"` 169 170 // DeviceID is the id of the client device, it is unique for same client. 171 DeviceID string `json:"device_id"` 172 173 DeviceName string `json:"device_name"` 174 175 Secrets *ClientSecrets `json:"secrets"` 176 177 // ConnectionID is the temporary connection id of the client, generated by the client. 178 ConnectionID string `json:"connection_id"` 179 180 // Timestamp of credentials creation. 181 Timestamp int64 `json:"timestamp"` 182 } 183 184 func (a *ClientAuthCredentials) validate() error { 185 186 return nil 187 }