forked from authorizerdev/authorizer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_session.go
More file actions
40 lines (33 loc) · 1.21 KB
/
validate_session.go
File metadata and controls
40 lines (33 loc) · 1.21 KB
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
package authorizer
import (
"encoding/json"
"fmt"
)
// ValidateSessionInput defines attributes for validate_session request
type ValidateSessionInput struct {
Cookie string `json:"cookie,omitempty"`
Roles []*string `json:"roles,omitempty"`
}
// ValidateSessionResponse defines attributes for validate_session response
type ValidateSessionResponse struct {
IsValid bool `json:"is_valid"`
User *User `json:"user,omitempty"`
}
// ValidateSession is method attached to AuthorizerClient.
// It performs validate_session query on authorizer instance.
// It returns ValidateSessionResponse reference or error.
// For implementation details check ValidateSessionExample examples/validate_session.go
func (c *AuthorizerClient) ValidateSession(req *ValidateSessionInput) (*ValidateSessionResponse, error) {
bytesData, err := c.ExecuteGraphQL(&GraphQLRequest{
Query: fmt.Sprintf(`query validateSession($data: ValidateSessionInput!){validate_session(params: $data) { is_valid user { %s } } }`, UserFragment),
Variables: map[string]interface{}{
"data": req,
},
}, nil)
if err != nil {
return nil, err
}
var res map[string]*ValidateSessionResponse
json.Unmarshal(bytesData, &res)
return res["validate_session"], nil
}