| 1 |
package centrifuge |
| 2 |
|
| 3 |
import "context" |
| 4 |
|
| 5 |
// Credentials allows to authenticate connection when set into context. |
| 6 |
type Credentials struct { |
| 7 |
// UserID tells library an ID of current user. Leave this empty string |
| 8 |
// if you need access from anonymous user. |
| 9 |
UserID string |
| 10 |
// ExpireAt allows to set time in future when connection must be validated. |
| 11 |
// In this case Client.OnRefresh callback must be set by application. Zero |
| 12 |
// value means no expiration. |
| 13 |
ExpireAt int64 |
| 14 |
// Info contains additional information about connection. This data will be |
| 15 |
// included untouched into Join/Leave messages, into Presence information, |
| 16 |
// also info can become a part of published message as part of ClientInfo. |
| 17 |
// In some cases having additional info can be an undesired overhead – but |
| 18 |
// you are simply free to not use this field at all. |
| 19 |
Info []byte |
| 20 |
} |
| 21 |
|
| 22 |
// credentialsContextKeyType is special type to safely use context for setting |
| 23 |
// and getting Credentials. |
| 24 |
type credentialsContextKeyType int |
| 25 |
|
| 26 |
// credentialsContextKey allows Go code to set Credentials into context. |
| 27 |
var credentialsContextKey credentialsContextKeyType |
| 28 |
|
| 29 |
// SetCredentials allows to set connection Credentials to Context. Credentials set |
| 30 |
// to Context in authentication middleware will be used by Centrifuge library to |
| 31 |
// authenticate user. |
| 32 |
func SetCredentials(ctx context.Context, cred *Credentials) context.Context { |
| 33 |
ctx = context.WithValue(ctx, credentialsContextKey, cred) |
| 34 |
return ctx |
| 35 |
} |
| 36 |
|
| 37 |
// GetCredentials allows to extract Credentials from Context (if set previously). |
| 38 |
func GetCredentials(ctx context.Context) (*Credentials, bool) { |
| 39 |
if val := ctx.Value(credentialsContextKey); val != nil { |
| 40 |
cred, ok := val.(*Credentials) |
| 41 |
return cred, ok |
| 42 |
} |
| 43 |
return nil, false |
| 44 |
} |