/
sessions.go
115 lines (96 loc) · 3.04 KB
/
sessions.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package clerk
import "fmt"
type SessionsService service
type Session struct {
Object string `json:"object"`
ID string `json:"id"`
ClientID string `json:"client_id"`
UserID string `json:"user_id"`
Status string `json:"status"`
LastActiveAt int64 `json:"last_active_at"`
LastActiveOrganizationID string `json:"last_active_organization_id,omitempty"`
ExpireAt int64 `json:"expire_at"`
AbandonAt int64 `json:"abandon_at"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
type ListAllSessionsParams struct {
Limit *int
Offset *int
ClientID *string
UserID *string
Status *SessionStatus
}
type SessionStatus string
const (
SessionStatusAbandoned SessionStatus = "abandoned"
SessionStatusActive SessionStatus = "active"
SessionStatusEnded SessionStatus = "ended"
SessionStatusExpired SessionStatus = "expired"
SessionStatusRemoved SessionStatus = "removed"
SessionStatusReplaced SessionStatus = "replaced"
SessionStatusRevoked SessionStatus = "revoked"
)
func (s *SessionsService) ListAll() ([]Session, error) {
sessionsUrl := "sessions"
req, _ := s.client.NewRequest("GET", sessionsUrl)
var sessions []Session
_, err := s.client.Do(req, &sessions)
if err != nil {
return nil, err
}
return sessions, nil
}
func (s *SessionsService) ListAllWithFiltering(params ListAllSessionsParams) ([]Session, error) {
sessionsUrl := "sessions"
req, _ := s.client.NewRequest("GET", sessionsUrl)
paginationParams := PaginationParams{Limit: params.Limit, Offset: params.Offset}
query := req.URL.Query()
addPaginationParams(query, paginationParams)
if params.ClientID != nil {
query.Add("client_id", *params.ClientID)
}
if params.UserID != nil {
query.Add("user_id", *params.UserID)
}
if params.Status != nil {
status := string(*params.Status)
query.Add("status", status)
}
req.URL.RawQuery = query.Encode()
var sessions []Session
_, err := s.client.Do(req, &sessions)
if err != nil {
return nil, err
}
return sessions, nil
}
func (s *SessionsService) Read(sessionId string) (*Session, error) {
sessionUrl := fmt.Sprintf("%s/%s", SessionsUrl, sessionId)
req, _ := s.client.NewRequest("GET", sessionUrl)
var session Session
_, err := s.client.Do(req, &session)
if err != nil {
return nil, err
}
return &session, nil
}
func (s *SessionsService) Revoke(sessionId string) (*Session, error) {
sessionUrl := fmt.Sprintf("%s/%s/revoke", SessionsUrl, sessionId)
req, _ := s.client.NewRequest("POST", sessionUrl)
var session Session
_, err := s.client.Do(req, &session)
if err != nil {
return nil, err
}
return &session, nil
}
func (s *SessionsService) Verify(sessionId, token string) (*Session, error) {
verifyUrl := fmt.Sprintf("%s/%s/verify", SessionsUrl, sessionId)
var sessionResponse Session
err := doVerify(s.client, verifyUrl, token, &sessionResponse)
if err != nil {
return nil, err
}
return &sessionResponse, nil
}