OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / net / http / authn / context.go
1 package authn
2
3 import (
4         "context"
5 )
6
7 type key int
8
9 const (
10         tokenKey key = iota
11         localhostKey
12 )
13
14 // newContextWithToken sets the token in a new context and returns the context.
15 func newContextWithToken(ctx context.Context, token string) context.Context {
16         return context.WithValue(ctx, tokenKey, token)
17 }
18
19 // Token returns the token stored in the context, if there is one.
20 func Token(ctx context.Context) string {
21         t, ok := ctx.Value(tokenKey).(string)
22         if !ok {
23                 return ""
24         }
25         return t
26 }
27
28 // newContextWithLocalhost sets the localhost flag to `true` in a new context
29 // and returns that context.
30 func newContextWithLocalhost(ctx context.Context) context.Context {
31         return context.WithValue(ctx, localhostKey, true)
32 }
33
34 // Localhost returns true if the localhost flag has been set.
35 func Localhost(ctx context.Context) bool {
36         l, ok := ctx.Value(localhostKey).(bool)
37         if ok && l {
38                 return true
39         }
40         return false
41 }