|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/url" |
| 8 | +) |
| 9 | + |
| 10 | +// AuthSession is a single active login session — an OAuth refresh-token family — |
| 11 | +// returned by entire-core's session endpoint. One is created per |
| 12 | +// `entire login`, across all of a user's devices. Plaintext token values are |
| 13 | +// never returned by the server, only metadata. (The list envelope's wire key |
| 14 | +// is "tokens"; the rows are sessions.) |
| 15 | +type AuthSession struct { |
| 16 | + ID string `json:"id"` |
| 17 | + UserID string `json:"user_id"` |
| 18 | + Name string `json:"name"` |
| 19 | + Scope string `json:"scope"` |
| 20 | + ExpiresAt string `json:"expires_at"` |
| 21 | + LastUsedAt *string `json:"last_used_at"` |
| 22 | + CreatedAt string `json:"created_at"` |
| 23 | +} |
| 24 | + |
| 25 | +// AuthSessionsResponse is the envelope returned by the list endpoint. |
| 26 | +type AuthSessionsResponse struct { |
| 27 | + Sessions []AuthSession `json:"tokens"` |
| 28 | +} |
| 29 | + |
| 30 | +// errAuthSessionsPathUnset surfaces when a session method is called on a Client |
| 31 | +// that wasn't given a base path. Construct via |
| 32 | +// NewClientWithBaseURL(...).WithAuthSessionsPath(...). |
| 33 | +var errAuthSessionsPathUnset = errors.New("api: auth sessions path is unset (call (*Client).WithAuthSessionsPath before list/revoke)") |
| 34 | + |
| 35 | +func (c *Client) authSessionsBasePath() (string, error) { |
| 36 | + if c.authSessionsPath == "" { |
| 37 | + return "", errAuthSessionsPathUnset |
| 38 | + } |
| 39 | + return c.authSessionsPath, nil |
| 40 | +} |
| 41 | + |
| 42 | +// ListAuthSessions returns the authenticated user's active login sessions. |
| 43 | +func (c *Client) ListAuthSessions(ctx context.Context) ([]AuthSession, error) { |
| 44 | + base, err := c.authSessionsBasePath() |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("list sessions: %w", err) |
| 47 | + } |
| 48 | + resp, err := c.Get(ctx, base) |
| 49 | + if err != nil { |
| 50 | + return nil, fmt.Errorf("list sessions: %w", err) |
| 51 | + } |
| 52 | + defer resp.Body.Close() |
| 53 | + |
| 54 | + if err := CheckResponse(resp); err != nil { |
| 55 | + return nil, fmt.Errorf("list sessions: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + var out AuthSessionsResponse |
| 59 | + if err := DecodeJSON(resp, &out); err != nil { |
| 60 | + return nil, fmt.Errorf("list sessions: %w", err) |
| 61 | + } |
| 62 | + return out.Sessions, nil |
| 63 | +} |
| 64 | + |
| 65 | +// RevokeCurrentAuthSession revokes the login session this client is authenticating |
| 66 | +// with (the family the current bearer belongs to). |
| 67 | +func (c *Client) RevokeCurrentAuthSession(ctx context.Context) error { |
| 68 | + base, err := c.authSessionsBasePath() |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("revoke current session: %w", err) |
| 71 | + } |
| 72 | + resp, err := c.Delete(ctx, base+"/current") |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("revoke current session: %w", err) |
| 75 | + } |
| 76 | + defer resp.Body.Close() |
| 77 | + |
| 78 | + if err := CheckResponse(resp); err != nil { |
| 79 | + return fmt.Errorf("revoke current session: %w", err) |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// RevokeAuthSession revokes the login session with the given id. |
| 85 | +func (c *Client) RevokeAuthSession(ctx context.Context, id string) error { |
| 86 | + base, err := c.authSessionsBasePath() |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("revoke session %s: %w", id, err) |
| 89 | + } |
| 90 | + resp, err := c.Delete(ctx, base+"/"+url.PathEscape(id)) |
| 91 | + if err != nil { |
| 92 | + return fmt.Errorf("revoke session %s: %w", id, err) |
| 93 | + } |
| 94 | + defer resp.Body.Close() |
| 95 | + |
| 96 | + if err := CheckResponse(resp); err != nil { |
| 97 | + return fmt.Errorf("revoke session %s: %w", id, err) |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
0 commit comments