-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
133 lines (110 loc) · 3.04 KB
/
context.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright 2022-2023 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package clu
import (
"net/http"
"time"
"github.com/tknie/services/auth"
)
// Audit callback function to be enabled
var Audit func(time.Time, *http.Request, error)
// Context server context
type Context struct {
User *auth.UserInfo
// User string
Pass string
Auth struct {
Roles []string
Remote string
Session interface{}
}
Token string
started time.Time
CurrentRequest *http.Request
dataMap map[string]any
}
// NewContextUserInfo new server context with user information and password
func NewContextUserInfo(userInfo *auth.UserInfo, pass string) *Context {
return &Context{
User: userInfo,
Auth: struct {
Roles []string
Remote string
Session interface{}
}{Session: auth.NewSessionInfo(userInfo.User)},
dataMap: make(map[string]any),
Pass: pass, started: time.Now()}
}
// NewContext new server context with user and password
func NewContext(user, pass string) *Context {
created := time.Now()
return NewContextUserInfo(&auth.UserInfo{User: user, Created: created}, pass)
}
// Deadline dead line
func (sc *Context) Deadline() (deadline time.Time, ok bool) { return time.Now(), false }
// Done context done
func (sc *Context) Done() <-chan struct{} { return make(<-chan struct{}) }
// Err error return
func (sc *Context) Err() error { return nil }
// Value value of key
func (sc *Context) Value(key any) any {
return nil
}
// UUID UUID interface function
func (sc *Context) UUID() string {
return sc.Auth.Session.(*auth.SessionInfo).UUID
}
// UserName user interface function
func (sc *Context) UserName() string {
return sc.User.User
}
// Name Name interface function
func (sc *Context) Name() string {
return sc.User.LongName
}
// AddRoles add roles interface function
func (sc *Context) AddRoles(r []string) {
sc.Auth.Roles = append(sc.Auth.Roles, r...)
}
// Remote remote info interface function
func (sc *Context) Remote() string {
return sc.Auth.Remote
}
// SetRemote set remote info interface function
func (sc *Context) SetRemote(r string) {
sc.Auth.Remote = r
}
// Roles roles info interface function
func (sc *Context) Roles() []string {
return sc.Auth.Roles
}
// Session session info interface function
func (sc *Context) Session() interface{} {
return sc.Auth.Session
}
// SetSession set session info interface function
func (sc *Context) SetSession(s interface{}) {
sc.Auth.Session = s
}
// SendAuditError send audit error in context
func (sc *Context) SendAuditError(started time.Time, err error) {
if Audit != nil {
Audit(started, sc.CurrentRequest, err)
}
}
// StoreData entry specific storage of data
func (sc *Context) StoreData(key string, value any) {
sc.dataMap[key] = value
}
// GetData entry specific storage of data is requested
func (sc *Context) GetData(key string) any {
return sc.dataMap[key]
}