/
session.go
106 lines (87 loc) · 2.99 KB
/
session.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
package auth
import (
"context"
"net/http"
"github.com/gofrs/uuid"
)
type authSessionKey string
const sessionContextKey authSessionKey = "session"
// Application describes the application name
type Application string
const (
// TspApp indicates tsp.move.mil
TspApp Application = "tsp"
// OfficeApp indicates office.move.mil
OfficeApp Application = "office"
// MilApp indicates my.move.mil (DNS still points to my.move.mil and not mil.move.mil)
MilApp Application = "mil"
// AdminApp indicates admin.move.mil
AdminApp Application = "admin"
)
// IsTspApp returns true iff the request is for the office.move.mil host
func (s *Session) IsTspApp() bool {
return s.ApplicationName == TspApp
}
// IsOfficeApp returns true iff the request is for the office.move.mil host
func (s *Session) IsOfficeApp() bool {
return s.ApplicationName == OfficeApp
}
// IsMilApp returns true iff the request is for the my.move.mil host
func (s *Session) IsMilApp() bool {
return s.ApplicationName == MilApp
}
// IsAdminApp returns true iff the request is for the admin.move.mil host
func (s *Session) IsAdminApp() bool {
return s.ApplicationName == AdminApp
}
// Session stores information about the currently logged in session
type Session struct {
ApplicationName Application
Hostname string
IDToken string
Disabled bool
UserID uuid.UUID
Email string
FirstName string
Middle string
LastName string
ServiceMemberID uuid.UUID
OfficeUserID uuid.UUID
TspUserID uuid.UUID
DpsUserID uuid.UUID
}
// SetSessionInRequestContext modifies the request's Context() to add the session data
func SetSessionInRequestContext(r *http.Request, session *Session) context.Context {
return context.WithValue(r.Context(), sessionContextKey, session)
}
// SetSessionInContext modifies the context to add the session data.
func SetSessionInContext(ctx context.Context, session *Session) context.Context {
return context.WithValue(ctx, sessionContextKey, session)
}
// SessionFromRequestContext gets the reference to the Session stored in the request.Context()
func SessionFromRequestContext(r *http.Request) *Session {
return SessionFromContext(r.Context())
}
// SessionFromContext gets the reference to the Session stored in the request.Context()
func SessionFromContext(ctx context.Context) *Session {
if session, ok := ctx.Value(sessionContextKey).(*Session); ok {
return session
}
return nil
}
// IsServiceMember checks whether the authenticated user is a ServiceMember
func (s *Session) IsServiceMember() bool {
return s.ServiceMemberID != uuid.Nil
}
// IsOfficeUser checks whether the authenticated user is an OfficeUser
func (s *Session) IsOfficeUser() bool {
return s.OfficeUserID != uuid.Nil
}
// IsTspUser checks whether the authenticated user is a TspUser
func (s *Session) IsTspUser() bool {
return s.TspUserID != uuid.Nil
}
// IsDpsUser checks whether the authenticated user is a DpsUser
func (s *Session) IsDpsUser() bool {
return s.DpsUserID != uuid.Nil
}