Repository navigation
Expand file tree
/
Copy pathauth.go
More file actions
264 lines (238 loc) · 8.1 KB
/
Copy pathauth.go
File metadata and controls
264 lines (238 loc) · 8.1 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package api
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/norcubeplatform/cli/internal/api/apierror"
"github.com/norcubeplatform/cli/internal/transport"
)
// AuthClient talks to the auth service (/auth/me, /organizations, ...).
// It expects the caller to provide a valid Bearer token via Authorization.
type AuthClient struct {
BaseURL string
HTTP *http.Client
Token func(ctx context.Context) (string, error)
}
func NewAuthClient(baseURL string, token func(ctx context.Context) (string, error)) *AuthClient {
return &AuthClient{
BaseURL: baseURL,
HTTP: transport.Default(),
Token: token,
}
}
type Me struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
AvatarURL string `json:"avatarUrl"`
}
type Organization struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
LogoURL *string `json:"logo_url"`
IsDefault bool `json:"is_default"`
}
func (c *AuthClient) GetMe(ctx context.Context) (*Me, error) {
var out Me
if err := c.do(ctx, http.MethodGet, "/auth/me", nil, &out); err != nil {
return nil, err
}
return &out, nil
}
func (c *AuthClient) ListOrganizations(ctx context.Context) ([]Organization, error) {
var out []Organization
if err := c.do(ctx, http.MethodGet, "/organizations", nil, &out); err != nil {
return nil, err
}
return out, nil
}
// CreateOrganization creates a new org with the given name. slug is
// optional — when nil or empty the server derives one from the name.
// The caller becomes the owner. Returns the new org's id, slug, and
// canonicalised name.
//
// The server returns the new org wrapped in {"organization": {...}}
// — we unwrap so the caller gets the same Organization shape that
// ListOrganizations hands back.
func (c *AuthClient) CreateOrganization(ctx context.Context, name string, slug *string) (*Organization, error) {
payload := struct {
Name string `json:"name"`
Slug *string `json:"slug,omitempty"`
}{Name: name, Slug: slug}
body, err := json.Marshal(payload)
if err != nil {
return nil, err
}
var resp struct {
Organization Organization `json:"organization"`
}
if err := c.do(ctx, http.MethodPost, "/organizations", bytes.NewReader(body), &resp); err != nil {
return nil, err
}
return &resp.Organization, nil
}
// Session is one signed-in CLI session of the caller (GET /auth/me/sessions).
type Session struct {
ID string `json:"id"`
Kind string `json:"kind"`
Hostname *string `json:"hostname"`
Client SessionClient `json:"client"`
IPAddress *string `json:"ip_address"`
CreatedAt time.Time `json:"created_at"`
LastUsedAt time.Time `json:"last_used_at"`
ExpiresAt time.Time `json:"expires_at"`
// Current marks the session the request's own token belongs to.
Current bool `json:"current"`
// OAuthGrantID and Scopes describe an OAuth sign-in; both are null
// for the older sign-in, which always has full access.
OAuthGrantID *string `json:"oauth_grant_id,omitempty"`
Scopes []string `json:"scopes,omitempty"`
}
// SessionClient is what the session's CLI reported about itself; fields
// are nil for CLIs that didn't identify themselves.
type SessionClient struct {
Name *string `json:"name"`
Version *string `json:"version"`
OS *string `json:"os"`
Arch *string `json:"arch"`
}
// ListSessions lists the caller's live CLI sessions, most recently used
// first.
func (c *AuthClient) ListSessions(ctx context.Context) ([]Session, error) {
var out []Session
if err := c.do(ctx, http.MethodGet, "/auth/me/sessions", nil, &out); err != nil {
return nil, err
}
return out, nil
}
// RevokeSession ends one of the caller's sessions. Idempotent for the
// caller's own sessions; an unknown id is a not-found error.
func (c *AuthClient) RevokeSession(ctx context.Context, id string) error {
return c.do(ctx, http.MethodDelete, "/auth/me/sessions/"+url.PathEscape(id), nil, nil)
}
// RevokeOtherSessions ends every session of the caller except the one the
// request's own token belongs to, and returns how many it ended.
func (c *AuthClient) RevokeOtherSessions(ctx context.Context) (int64, error) {
var out struct {
Revoked int64 `json:"revoked"`
}
if err := c.do(ctx, http.MethodPost, "/auth/me/sessions/revoke-others", nil, &out); err != nil {
return 0, err
}
return out.Revoked, nil
}
// CIToken is an organization's CI token as Norcube Auth lists it. The
// secret itself is only ever returned once, by CreateCIToken.
type CIToken struct {
ID string `json:"id"`
Name string `json:"name"`
Preview string `json:"preview"`
Scopes []string `json:"scopes"`
CreatedBy *string `json:"created_by"`
CreatedAt time.Time `json:"created_at"`
ExpiresAt time.Time `json:"expires_at"`
LastUsedAt *time.Time `json:"last_used_at"`
LastUsedIP *string `json:"last_used_ip"`
RevokedAt *time.Time `json:"revoked_at"`
// Status is active, expired or revoked.
Status string `json:"status"`
}
// CITokenScope is a scope a CI token can carry, and whether the caller's
// role allows granting it.
type CITokenScope struct {
Scope string `json:"scope"`
Label string `json:"label"`
Description string `json:"description"`
Product string `json:"product"`
Write bool `json:"write"`
Available bool `json:"available"`
}
// CreatedCIToken is a new CI token plus its secret, shown once.
type CreatedCIToken struct {
Token CIToken `json:"token"`
Secret string `json:"secret"`
}
// ListCITokens lists the organization's CI tokens (the organization of the
// client's token).
func (c *AuthClient) ListCITokens(ctx context.Context) ([]CIToken, error) {
var out []CIToken
if err := c.do(ctx, http.MethodGet, "/iam/ci-tokens", nil, &out); err != nil {
return nil, err
}
return out, nil
}
// ListCITokenScopes lists every CI token scope and whether the caller may
// grant it.
func (c *AuthClient) ListCITokenScopes(ctx context.Context) ([]CITokenScope, error) {
var out []CITokenScope
if err := c.do(ctx, http.MethodGet, "/iam/ci-tokens/scopes", nil, &out); err != nil {
return nil, err
}
return out, nil
}
// CreateCIToken creates a CI token in the organization and returns it with
// its secret.
func (c *AuthClient) CreateCIToken(ctx context.Context, name string, scopes []string, expiresAt time.Time) (*CreatedCIToken, error) {
body, err := json.Marshal(struct {
Name string `json:"name"`
Scopes []string `json:"scopes"`
ExpiresAt time.Time `json:"expires_at"`
}{name, scopes, expiresAt.UTC()})
if err != nil {
return nil, err
}
var out CreatedCIToken
if err := c.do(ctx, http.MethodPost, "/iam/ci-tokens", bytes.NewReader(body), &out); err != nil {
return nil, err
}
return &out, nil
}
// RevokeCIToken revokes a CI token; revoking a revoked token is a no-op.
func (c *AuthClient) RevokeCIToken(ctx context.Context, id string) (*CIToken, error) {
var out CIToken
if err := c.do(ctx, http.MethodDelete, "/iam/ci-tokens/"+url.PathEscape(id), nil, &out); err != nil {
return nil, err
}
return &out, nil
}
func (c *AuthClient) do(ctx context.Context, method, path string, body io.Reader, out any) error {
token, err := c.Token(ctx)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, method, c.BaseURL+path, body)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/json")
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.HTTP.Do(req)
if err != nil {
return fmt.Errorf("%s %s: %w", method, path, err)
}
defer resp.Body.Close()
raw, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
return apierror.Format("auth", resp, raw)
}
if out == nil {
return nil
}
if err := json.Unmarshal(raw, out); err != nil {
return fmt.Errorf("decode %s %s: %w", method, path, err)
}
return nil
}
// ErrUnauthorized is returned when the API rejects our token. Higher-level
// commands can suggest re-running `norcube login`.
var ErrUnauthorized = errors.New("unauthorized — your session may have expired, run `norcube login`")