Skip to content

Commit f5db0e9

Browse files
committed
JWT is received via state-svc
1 parent 010bfe5 commit f5db0e9

File tree

17 files changed

+1044
-155
lines changed

17 files changed

+1044
-155
lines changed

cmd/state-svc/internal/resolver/resolver.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ type Resolver struct {
4040
anForClient *sync.Client // Use separate client for events sent through service so we don't contaminate one with the other
4141
rtwatch *rtwatcher.Watcher
4242
auth *authentication.Auth
43+
44+
// mostRecentActivity records the most recent user activity that was sent to the resolver.
45+
// This is meant to focus on user activity. If ever we start polling the svc without user activity then the
46+
// intelligence behind this will need to be updated.
47+
mostRecentActivity *time.Time
4348
}
4449

50+
// jwtKeepAliveDuration determines how long after the last state tool interaction we want to keep the JWT alive
51+
const jwtKeepAliveDuration = 1 * time.Hour
52+
4553
// var _ genserver.ResolverRoot = &Resolver{} // Must implement ResolverRoot
4654

4755
func New(cfg *config.Instance, an *sync.Client, auth *authentication.Auth) (*Resolver, error) {
@@ -65,9 +73,12 @@ func New(cfg *config.Instance, an *sync.Client, auth *authentication.Auth) (*Res
6573
pollRate = overrideInt
6674
}
6775

68-
pollAuth := poller.New(time.Duration(int64(time.Millisecond)*pollRate), func() (interface{}, error) {
69-
if auth.SyncRequired() {
70-
return nil, auth.Sync()
76+
pollRateDuration := time.Duration(int64(time.Millisecond) * pollRate)
77+
78+
mostRecentActivity := ptr.To(time.Now())
79+
pollAuth := poller.New(pollRateDuration, func() (interface{}, error) {
80+
if err := auth.MaybeRenew(time.Now().Add(pollRateDuration)); err != nil {
81+
return nil, errs.Wrap(err, "Could not renew auth")
7182
}
7283
return nil, nil
7384
})
@@ -85,6 +96,7 @@ func New(cfg *config.Instance, an *sync.Client, auth *authentication.Auth) (*Res
8596
anForClient,
8697
rtwatcher.New(cfg, anForClient),
8798
auth,
99+
mostRecentActivity,
88100
}, nil
89101
}
90102

@@ -98,7 +110,10 @@ func (r *Resolver) Close() error {
98110

99111
// Seems gqlgen supplies this so you can separate your resolver and query resolver logic
100112
// So far no need for this, so we're pointing back at ourselves..
101-
func (r *Resolver) Query() genserver.QueryResolver { return r }
113+
func (r *Resolver) Query() genserver.QueryResolver {
114+
*r.mostRecentActivity = time.Now()
115+
return r
116+
}
102117

103118
func (r *Resolver) Version(ctx context.Context) (*graph.Version, error) {
104119
defer func() { handlePanics(recover(), debug.Stack()) }()
@@ -262,6 +277,40 @@ func (r *Resolver) GetProcessesInUse(ctx context.Context, execDir string) ([]*gr
262277
return processes, nil
263278
}
264279

280+
func (r *Resolver) GetJwt(ctx context.Context) (*graph.Jwt, error) {
281+
if r.auth.SyncRequired() {
282+
return nil, r.auth.Sync()
283+
}
284+
285+
if !r.auth.Authenticated() {
286+
return nil, nil
287+
}
288+
289+
user := r.auth.User()
290+
if user == nil {
291+
return nil, errs.New("user is nil")
292+
}
293+
294+
jwt := &graph.Jwt{
295+
Token: r.auth.BearerToken(),
296+
User: &graph.User{
297+
UserID: user.UserID.String(),
298+
Username: user.Username,
299+
Email: user.Email,
300+
Organizations: []*graph.Organization{},
301+
},
302+
}
303+
304+
for _, org := range user.Organizations {
305+
jwt.User.Organizations = append(jwt.User.Organizations, &graph.Organization{
306+
URLname: org.URLname,
307+
Role: org.Role,
308+
})
309+
}
310+
311+
return jwt, nil
312+
}
313+
265314
func handlePanics(recovered interface{}, stack []byte) {
266315
if recovered != nil {
267316
multilog.Error("Panic: %v", recovered)

0 commit comments

Comments
 (0)