...
1
15 package auth
16
17 import (
18 "fmt"
19 apb "golang.conradwood.net/apis/auth"
20
21 "context"
22
23 "golang.conradwood.net/go-easyops/common"
24 "golang.conradwood.net/go-easyops/ctx"
25 "golang.conradwood.net/go-easyops/rpc"
26 )
27
28
29 func GetOrganisationID(uctx context.Context) string {
30 return "1"
31 }
32
33
34 func GetUser(uctx context.Context) *apb.User {
35 u := ctx.GetLocalState(uctx).User()
36 us := common.VerifySignedUser(u)
37 if us != nil {
38
39 return us
40 }
41
42 cs := rpc.CallStateFromContext(uctx)
43 if cs == nil {
44 return nil
45 }
46 return cs.User()
47 }
48
49
50 func GetSignedUser(uctx context.Context) *apb.SignedUser {
51 u := ctx.GetLocalState(uctx).User()
52 if u != nil {
53
54 return u
55 }
56
57 cs := rpc.CallStateFromContext(uctx)
58 if cs == nil {
59 return nil
60 }
61 su := cs.SignedUser()
62 if su == nil && cs.User() != nil {
63 panic("mismatched old style context, no signed user, but unsigned user present")
64 }
65 return su
66 }
67
68
69 func GetSignedService(uctx context.Context) *apb.SignedUser {
70 u := ctx.GetLocalState(uctx).CallingService()
71 if u != nil {
72 return u
73 }
74
75 cs := rpc.CallStateFromContext(uctx)
76 if cs == nil {
77 return nil
78 }
79 res := cs.SignedService()
80 if res == nil && cs.CallerService() != nil {
81 panic("invalid callstate (no signed service, but unsignedservice)")
82 }
83 return res
84 }
85
86
87 func GetService(uctx context.Context) *apb.User {
88 return common.VerifySignedUser(GetSignedService(uctx))
89 }
90
91
92 func GetCreatingService(uctx context.Context) *apb.User {
93 u := ctx.GetLocalState(uctx).CreatorService()
94 us := common.VerifySignedUser(u)
95 return us
96
97 }
98
99 func PrintUser(u *apb.User) {
100 if u == nil {
101 return
102 }
103 fmt.Printf("User ID: %s\n", u.ID)
104 fmt.Printf(" Email: %s\n", u.Email)
105 fmt.Printf(" Abbrev:%s\n", u.Abbrev)
106 }
107 func PrintSignedUser(uu *apb.SignedUser) {
108 u := common.VerifySignedUser(uu)
109 if u == nil {
110 return
111 }
112
113 fmt.Printf("User ID: %s\n", u.ID)
114 fmt.Printf(" Email: %s\n", u.Email)
115 fmt.Printf(" Abbrev:%s\n", u.Abbrev)
116 }
117
118
119 func SignedDescription(user *apb.SignedUser) string {
120 u := common.VerifySignedUser(user)
121 return Description(u)
122 }
123 func Description(user *apb.User) string {
124 if user == nil {
125 return "ANONYMOUS"
126 }
127 if user.Abbrev != "" {
128 return user.Abbrev
129 }
130 if user.Email != "" {
131 return user.Email
132 }
133 return "user #" + user.ID
134 }
135
136
137 func UserIDString(user *apb.User) string {
138 if user == nil {
139 return "ANONYMOUS"
140 }
141 if user.Abbrev != "" {
142 return "#" + user.ID + " (" + user.Abbrev + ")"
143 }
144 if user.Email != "" {
145 return "#" + user.ID + " (" + user.Email + ")"
146 }
147 return "user #" + user.ID
148 }
149
150
151 func CurrentUserString(ctx context.Context) string {
152 u := GetUser(ctx)
153 if u == nil {
154 return "ANONYMOUS"
155 }
156 return fmt.Sprintf("User #%s (%s)", u.ID, u.Email)
157 }
158
View as plain text