You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the websocket's configured initialization function is ran, and returns a modified context with a deadline set to the expiration time of a JWT (JSON Web Token), the websocket is not disconnected when the context is done/cancelled.
What did you expect?
That the websocket would be disconnected.
Versions
gqlgen version? v0.14.0
go version? 1.17.3 darwin/amd64
dep or go modules? Modules!
The Actual Proposal
Let the websocket connection close when the context is done.
For this reason it would also be nice to add a way to communicate back to the user that the token has expired. Maybe through a context value like this?
// A private key for context that only this package can access. This is important// to prevent collisions between different context usesvarcloseReasonCtxKey=&wsCloseReasonContextKey{"close-reason"}
typewsCloseReasonContextKeystruct {
namestring
}
funcAppendCloseReason(ctx context.Context, reasonstring) context.Context {
returncontext.WithValue(ctx, closeReasonCtxKey, reason)
}
funccloseReasonForContext(ctx context.Context) string {
reason, _:=ctx.Value(closeReasonCtxKey).(string)
returnreason
}
func (c*wsConnection) closeOnCancel(ctx context.Context) {
<-ctx.Done()
ifr:=closeReasonForContext(ctx); r!="" {
c.sendConnectionError(r)
}
c.close(websocket.CloseNormalClosure, "terminated")
}
This way, the application can add a reason to the context values like so:
funcWebsocketAuthorization(ctx context.Context, initPayload transport.InitPayload) (context.Context, error) {
payloadAuth:=initPayload.Authorization()
ifpayloadAuth=="" {
returnctx, errors.New("the JWT is missing in the initialization payload")
}
jwt, err:=authenticateJWT(payloadAuth)
iferr!=nil {
returnctx, err
}
// Add the JWT expiration as a deadline, and add the reasonnewCtx, _:=context.WithDeadline(transport.AppendCloseReason(ctx, "authentication token has expired"), time.Unix(jwt.ExpiresAt, 0))
returnnewCtx, nil
}
Sidenote
This proposal would also solve this (pretty old) issue #774.
The text was updated successfully, but these errors were encountered:
Scenario
What happened?
When the websocket's configured initialization function is ran, and returns a modified context with a deadline set to the expiration time of a JWT (JSON Web Token), the websocket is not disconnected when the context is done/cancelled.
What did you expect?
That the websocket would be disconnected.
Versions
gqlgen version
?v0.14.0
go version
?1.17.3 darwin/amd64
The Actual Proposal
Let the websocket connection close when the context is done.
Example
Add the following function to the
wsConnection
type:And then, call it from the
wsConnection
's run function like so:More feedback = more better
For this reason it would also be nice to add a way to communicate back to the user that the token has expired. Maybe through a context value like this?
This way, the application can add a reason to the context values like so:
Sidenote
This proposal would also solve this (pretty old) issue #774.
The text was updated successfully, but these errors were encountered: