-
Notifications
You must be signed in to change notification settings - Fork 34
Flexible Authentication Hook #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
|
@@ -47,18 +48,28 @@ type startMessagePayload struct { | |
Variables map[string]interface{} `json:"variables"` | ||
} | ||
|
||
type initMessagePayload struct{} | ||
|
||
// GraphQLService interface | ||
type GraphQLService interface { | ||
Subscribe(ctx context.Context, document string, operationName string, variableValues map[string]interface{}) (payloads <-chan interface{}, err error) | ||
} | ||
|
||
type AuthenticateFunc func(ctx context.Context, r *http.Request, payload map[string]interface{}) error | ||
|
||
type connection struct { | ||
cancel func() | ||
service GraphQLService | ||
writeTimeout time.Duration | ||
ws wsConnection | ||
cancel func() | ||
service GraphQLService | ||
writeTimeout time.Duration | ||
ws wsConnection | ||
authenticated bool | ||
authenticateFunc AuthenticateFunc | ||
request *http.Request | ||
} | ||
|
||
func Authentication(r *http.Request, f AuthenticateFunc) func(conn *connection) { | ||
return func(conn *connection) { | ||
conn.authenticateFunc = f | ||
conn.request = r | ||
} | ||
} | ||
|
||
// ReadLimit limits the maximum size of incoming messages | ||
|
@@ -159,15 +170,31 @@ func (conn *connection) readLoop(ctx context.Context, send sendFunc) { | |
|
||
switch msg.Type { | ||
case typeConnectionInit: | ||
var initMsg initMessagePayload | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for empty line here. Please, remove it. |
||
var initMsg map[string]interface{} | ||
if err := json.Unmarshal(msg.Payload, &initMsg); err != nil { | ||
ep := errPayload(fmt.Errorf("invalid payload for type: %s", msg.Type)) | ||
send("", typeConnectionError, ep) | ||
continue | ||
} | ||
|
||
if conn.authenticateFunc != nil { | ||
if err := conn.authenticateFunc(ctx, conn.request, initMsg); err != nil { | ||
send("", typeConnectionError, errPayload(err)) | ||
continue | ||
} | ||
} | ||
conn.authenticated = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if conn.authenticated = true inside of the |
||
send("", typeConnectionAck, nil) | ||
|
||
case typeStart: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary empty line. Please, remove it. |
||
if !conn.authenticated && conn.authenticateFunc != nil { | ||
ep := errPayload(errors.New("authentication required.")) | ||
send("", typeConnectionError, ep) | ||
continue | ||
} | ||
|
||
// TODO: check an operation with the same ID hasn't been started already | ||
if msg.ID == "" { | ||
ep := errPayload(errors.New("missing ID for start operation")) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need the request as a field of the connection?