File: identity.go

package info (click to toggle)
golang-github-jcmturner-goidentity.v6 6.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 140 kB
  • sloc: makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,059 bytes parent folder | download | duplicates (2)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package goidentity

import (
	"context"
	"net/http"
	"time"
)

const (
	CTXKey = "jcmturner/goidentity"
)

type Identity interface {
	UserName() string
	SetUserName(s string)
	Domain() string
	SetDomain(s string)
	DisplayName() string
	SetDisplayName(s string)
	Human() bool
	SetHuman(b bool)
	AuthTime() time.Time
	SetAuthTime(t time.Time)
	AuthzAttributes() []string
	AddAuthzAttribute(a string)
	RemoveAuthzAttribute(a string)
	Authenticated() bool
	SetAuthenticated(b bool)
	Authorized(a string) bool
	SessionID() string
	Expired() bool
	Attributes() map[string]interface{}
	SetAttribute(k string, v interface{})
	SetAttributes(map[string]interface{})
	RemoveAttribute(k string)
	Marshal() ([]byte, error)
	Unmarshal([]byte) error
}

func AddToHTTPRequestContext(id Identity, r *http.Request) *http.Request {
	ctx := r.Context()
	ctx = context.WithValue(ctx, CTXKey, id)
	return r.WithContext(ctx)
}

func FromHTTPRequestContext(r *http.Request) Identity {
	ctx := r.Context()
	if id, ok := ctx.Value(CTXKey).(Identity); ok {
		return id
	}
	return nil
}