authenticate tries to authenticate the user by selecting appropriate authenticator function.
()
| 180 | // authenticate tries to authenticate the user by selecting appropriate |
| 181 | // authenticator function. |
| 182 | func (r *Request) authenticate() *Error { |
| 183 | // Trust the Kite if we have initiated the connection. Following casts |
| 184 | // means, session is opened by the client. |
| 185 | if _, ok := r.Client.session.(*sockjsclient.WebsocketSession); ok { |
| 186 | return nil |
| 187 | } |
| 188 | |
| 189 | if _, ok := r.Client.session.(*sockjsclient.XHRSession); ok { |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | if r.Auth == nil { |
| 194 | return &Error{ |
| 195 | Type: "authenticationError", |
| 196 | Message: "No authentication information is provided", |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Select authenticator function. |
| 201 | f := r.LocalKite.Authenticators[r.Auth.Type] |
| 202 | if f == nil { |
| 203 | return &Error{ |
| 204 | Type: "authenticationError", |
| 205 | Message: fmt.Sprintf("Unknown authentication type: %s", r.Auth.Type), |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Call authenticator function. It sets the Request.Username field. |
| 210 | err := f(r) |
| 211 | if err != nil { |
| 212 | return &Error{ |
| 213 | Type: "authenticationError", |
| 214 | Message: fmt.Sprintf("%s: %s", r.Auth.Type, err), |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Replace username of the remote Kite with the username that client send |
| 219 | // us. This prevents a Kite to impersonate someone else's Kite. |
| 220 | r.Client.SetUsername(r.Username) |
| 221 | return nil |
| 222 | } |
| 223 | |
| 224 | // AuthenticateFromToken is the default Authenticator for Kite. |
| 225 | func (k *Kite) AuthenticateFromToken(r *Request) error { |