forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
33 lines (27 loc) · 767 Bytes
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package auth
import (
"context"
)
type contextKey int
const (
authContextKey contextKey = iota
)
// Auth holds a set of details that have been authenticated about a client.
type Auth struct {
Address string
PhoneNumber string
Email string
}
// FromContext returns auth details that are stored in the context.
func FromContext(ctx context.Context) (Auth, bool) {
if a, ok := ctx.Value(authContextKey).(Auth); ok {
return a, true
}
return Auth{}, false
}
// NewContext returns a new context that is a copy of the given context with
// the auth details set within. An Auth can be retrieved from the context using
// FromContext.
func NewContext(ctx context.Context, a Auth) context.Context {
return context.WithValue(ctx, authContextKey, a)
}