Class: Appwrite::Oauth2

Inherits:
Service show all
Defined in:
lib/appwrite/services/oauth2.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Oauth2

Returns a new instance of Oauth2.



6
7
8
# File 'lib/appwrite/services/oauth2.rb', line 6

def initialize(client)
    @client = client
end

Instance Method Details

#approve(grant_id:, authorization_details: nil, scope: nil) ⇒ Oauth2Approve

Approve an OAuth2 grant after the user gives consent. Returns the redirectUrl the end user should be sent to. The consent screen may optionally pass enriched authorization_details to record the concrete resources the user selected. You can pass Accept header of application/json to receive a JSON response instead of a redirect.

Parameters:

  • grant_id (String)

    Grant ID made during authorization, provided to consent screen in URL search params.

  • authorization_details (String) (defaults to: nil)

    Enriched authorization_details the user consented to, replacing what the client requested. Each entry must use a type the project accepts. Optional; omit to keep the originally requested details.

  • scope (String) (defaults to: nil)

    Space-separated scopes the user consented to. Must be a subset of the scopes originally requested; identity scopes such as openid are always retained. Optional; omit to keep the originally requested scopes.

Returns:

  • (Oauth2Approve)


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
# File 'lib/appwrite/services/oauth2.rb', line 21

def approve(grant_id:, authorization_details: nil, scope: nil)
    api_path = '/oauth2/{project_id}/approve'
        .gsub('{project_id}', @client.get_config('project'))

    if grant_id.nil?
      raise Appwrite::Exception.new('Missing required parameter: "grantId"')
    end

    api_params = {
        grant_id: grant_id,
        authorization_details: authorization_details,
        scope: scope,
    }
    
    api_headers = {
        "content-type": 'application/json',
        "accept": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2Approve
    )

end

#authorize(client_id: nil, redirect_uri: nil, response_type: nil, scope: nil, state: nil, nonce: nil, code_challenge: nil, code_challenge_method: nil, prompt: nil, max_age: nil, authorization_details: nil, resource: nil, audience: nil, request_uri: nil) ⇒ Oauth2Authorize

Begin the OAuth2 authorization flow. When called without a session, the user is redirected to the consent screen without grant ID. When called with a session, the redirect URL includes param for grant ID. You can pass Accept header of application/json to receive a JSON response instead of a redirect.

Parameters:

  • client_id (String) (defaults to: nil)

    OAuth2 client ID. Either a registered app ID or an HTTPS client ID metadata document URL.

  • redirect_uri (String) (defaults to: nil)

    Redirect URI where visitor will be redirected after authorization, whether successful or not.

  • response_type (String) (defaults to: nil)

    OAuth2 / OIDC response type. One of code (Authorization Code Flow), id_token (Implicit Flow, OIDC login only), or code id_token (Hybrid Flow).

  • scope (String) (defaults to: nil)

    Space-separated OAuth2 scopes. Can include project scopes, and built-in scopes: openid, email, profile, phone.

  • state (String) (defaults to: nil)

    OAuth2 state. You receive this back in the redirect URI.

  • nonce (String) (defaults to: nil)

    OIDC nonce parameter to prevent replay attacks. Required when response_type includes id_token.

  • code_challenge (String) (defaults to: nil)

    PKCE code challenge. Required when OAuth2 app is public.

  • code_challenge_method (String) (defaults to: nil)

    PKCE code challenge method. Required when OAuth2 app is public.

  • prompt (String) (defaults to: nil)

    OIDC prompt parameter for customization of consent screen. Space-separated list of: none, login, consent, select_account.

  • max_age (Integer) (defaults to: nil)

    OIDC max_age paraleter for customization of consent screen. Maximum allowable elapsed time in seconds since the user last authenticated. If exceeded, re-authentication is required.

  • authorization_details (String) (defaults to: nil)

    Rich authorization request. JSON array of objects, each with a type and project-defined fields

  • resource (String) (defaults to: nil)

    RFC 8707 resource indicator URI or URI list. Each value must be an absolute URI without a fragment.

  • audience (String) (defaults to: nil)

    Compatibility alias for a single OAuth2 resource indicator URI.

  • request_uri (String) (defaults to: nil)

    OAuth2 authorization request handle returned by the pushed authorization request endpoint.

Returns:

  • (Oauth2Authorize)


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
# File 'lib/appwrite/services/oauth2.rb', line 72

def authorize(client_id: nil, redirect_uri: nil, response_type: nil, scope: nil, state: nil, nonce: nil, code_challenge: nil, code_challenge_method: nil, prompt: nil, max_age: nil, authorization_details: nil, resource: nil, audience: nil, request_uri: nil)
    api_path = '/oauth2/{project_id}/authorize'
        .gsub('{project_id}', @client.get_config('project'))

    api_params = {
        client_id: client_id,
        redirect_uri: redirect_uri,
        response_type: response_type,
        scope: scope,
        state: state,
        nonce: nonce,
        code_challenge: code_challenge,
        code_challenge_method: code_challenge_method,
        prompt: prompt,
        max_age: max_age,
        authorization_details: authorization_details,
        resource: resource,
        audience: audience,
        request_uri: request_uri,
    }
    
    api_headers = {
        "accept": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2Authorize
    )

end

#authorize_post(client_id: nil, redirect_uri: nil, response_type: nil, scope: nil, state: nil, nonce: nil, code_challenge: nil, code_challenge_method: nil, prompt: nil, max_age: nil, authorization_details: nil, resource: nil, audience: nil, request_uri: nil) ⇒ Oauth2Authorize

Begin the OAuth2 authorization flow. When called without a session, the user is redirected to the consent screen without grant ID. When called with a session, the redirect URL includes param for grant ID. You can pass Accept header of application/json to receive a JSON response instead of a redirect.

Parameters:

  • client_id (String) (defaults to: nil)

    OAuth2 client ID. Either a registered app ID or an HTTPS client ID metadata document URL.

  • redirect_uri (String) (defaults to: nil)

    Redirect URI where visitor will be redirected after authorization, whether successful or not.

  • response_type (String) (defaults to: nil)

    OAuth2 / OIDC response type. One of code (Authorization Code Flow), id_token (Implicit Flow, OIDC login only), or code id_token (Hybrid Flow).

  • scope (String) (defaults to: nil)

    Space-separated OAuth2 scopes. Can include project scopes, and built-in scopes: openid, email, profile, phone.

  • state (String) (defaults to: nil)

    OAuth2 state. You receive this back in the redirect URI.

  • nonce (String) (defaults to: nil)

    OIDC nonce parameter to prevent replay attacks. Required when response_type includes id_token.

  • code_challenge (String) (defaults to: nil)

    PKCE code challenge. Required when OAuth2 app is public.

  • code_challenge_method (String) (defaults to: nil)

    PKCE code challenge method. Required when OAuth2 app is public.

  • prompt (String) (defaults to: nil)

    OIDC prompt parameter for customization of consent screen. Space-separated list of: none, login, consent, select_account.

  • max_age (Integer) (defaults to: nil)

    OIDC max_age paraleter for customization of consent screen. Maximum allowable elapsed time in seconds since the user last authenticated. If exceeded, re-authentication is required.

  • authorization_details (String) (defaults to: nil)

    Rich authorization request. JSON array of objects, each with a type and project-defined fields

  • resource (String) (defaults to: nil)

    RFC 8707 resource indicator URI or URI list. Each value must be an absolute URI without a fragment.

  • audience (String) (defaults to: nil)

    Compatibility alias for a single OAuth2 resource indicator URI.

  • request_uri (String) (defaults to: nil)

    OAuth2 authorization request handle returned by the pushed authorization request endpoint.

Returns:

  • (Oauth2Authorize)


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
# File 'lib/appwrite/services/oauth2.rb', line 129

def authorize_post(client_id: nil, redirect_uri: nil, response_type: nil, scope: nil, state: nil, nonce: nil, code_challenge: nil, code_challenge_method: nil, prompt: nil, max_age: nil, authorization_details: nil, resource: nil, audience: nil, request_uri: nil)
    api_path = '/oauth2/{project_id}/authorize'
        .gsub('{project_id}', @client.get_config('project'))

    api_params = {
        client_id: client_id,
        redirect_uri: redirect_uri,
        response_type: response_type,
        scope: scope,
        state: state,
        nonce: nonce,
        code_challenge: code_challenge,
        code_challenge_method: code_challenge_method,
        prompt: prompt,
        max_age: max_age,
        authorization_details: authorization_details,
        resource: resource,
        audience: audience,
        request_uri: request_uri,
    }
    
    api_headers = {
        "content-type": 'application/json',
        "accept": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2Authorize
    )

end

#create_device_authorization(client_id: nil, scope: nil, authorization_details: nil, resource: nil, audience: nil) ⇒ Oauth2DeviceAuthorization

Start the OAuth2 Device Authorization Grant. Returns the device code, user code, verification URL, expiration, and polling interval.

Parameters:

  • client_id (String) (defaults to: nil)

    OAuth2 client ID. Either a registered app ID or an HTTPS client ID metadata document URL.

  • scope (String) (defaults to: nil)

    Space-separated OAuth2 scopes. Can include project scopes, and built-in scopes: openid, email, profile.

  • authorization_details (String) (defaults to: nil)

    Rich authorization request. JSON array of objects, each with a type and project-defined fields

  • resource (String) (defaults to: nil)

    RFC 8707 resource indicator URI or URI list. Each value must be an absolute URI without a fragment.

  • audience (String) (defaults to: nil)

    Compatibility alias for a single OAuth2 resource indicator URI.

Returns:

  • (Oauth2DeviceAuthorization)


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
# File 'lib/appwrite/services/oauth2.rb', line 175

def create_device_authorization(client_id: nil, scope: nil, authorization_details: nil, resource: nil, audience: nil)
    api_path = '/oauth2/{project_id}/device_authorization'
        .gsub('{project_id}', @client.get_config('project'))

    api_params = {
        client_id: client_id,
        scope: scope,
        authorization_details: authorization_details,
        resource: resource,
        audience: audience,
    }
    
    api_headers = {
        "content-type": 'application/json',
        "accept": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2DeviceAuthorization
    )

end

#create_grant(user_code:) ⇒ Oauth2Grant

Exchange a device flow user code for an OAuth2 grant. The authenticated user is bound to the pending grant. Pass the returned grant ID to the get grant endpoint to render the consent screen, then to the approve or reject endpoint to complete the flow.

Parameters:

  • user_code (String)

    User code displayed on the device.

Returns:

  • (Oauth2Grant)


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
# File 'lib/appwrite/services/oauth2.rb', line 210

def create_grant(user_code:)
    api_path = '/oauth2/{project_id}/grants'
        .gsub('{project_id}', @client.get_config('project'))

    if user_code.nil?
      raise Appwrite::Exception.new('Missing required parameter: "userCode"')
    end

    api_params = {
        user_code: user_code,
    }
    
    api_headers = {
        "content-type": 'application/json',
        "accept": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2Grant
    )

end

#create_par(client_id:, redirect_uri:, response_type:, scope: nil, state: nil, nonce: nil, code_challenge: nil, code_challenge_method: nil, prompt: nil, max_age: nil, authorization_details: nil, resource: nil, audience: nil) ⇒ Oauth2PAR

Store an OAuth2 authorization request server-side and receive a short-lived request_uri handle for the authorize endpoint.

Parameters:

  • client_id (String)

    OAuth2 client ID. Either a registered app ID or an HTTPS client ID metadata document URL.

  • redirect_uri (String)

    Redirect URI where visitor will be redirected after authorization, whether successful or not.

  • response_type (String)

    OAuth2 / OIDC response type.

  • scope (String) (defaults to: nil)

    Space-separated OAuth2 scopes. Can include project scopes, and built-in scopes: openid, email, profile, phone.

  • state (String) (defaults to: nil)

    OAuth2 state. You receive this back in the redirect URI.

  • nonce (String) (defaults to: nil)

    OIDC nonce parameter to prevent replay attacks. Required when response_type includes id_token.

  • code_challenge (String) (defaults to: nil)

    PKCE code challenge. Required when OAuth2 app is public.

  • code_challenge_method (String) (defaults to: nil)

    PKCE code challenge method. Required when OAuth2 app is public.

  • prompt (String) (defaults to: nil)

    OIDC prompt parameter for customization of consent screen. Space-separated list of: none, login, consent, select_account.

  • max_age (Integer) (defaults to: nil)

    OIDC max_age parameter for customization of consent screen.

  • authorization_details (String) (defaults to: nil)

    Rich authorization request. JSON array of objects, each with a type and project-defined fields

  • resource (String) (defaults to: nil)

    RFC 8707 resource indicator URI or URI list. Each value must be an absolute URI without a fragment.

  • audience (String) (defaults to: nil)

    Compatibility alias for a single OAuth2 resource indicator URI.

Returns:

  • (Oauth2PAR)


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/appwrite/services/oauth2.rb', line 321

def create_par(client_id:, redirect_uri:, response_type:, scope: nil, state: nil, nonce: nil, code_challenge: nil, code_challenge_method: nil, prompt: nil, max_age: nil, authorization_details: nil, resource: nil, audience: nil)
    api_path = '/oauth2/{project_id}/par'
        .gsub('{project_id}', @client.get_config('project'))

    if client_id.nil?
      raise Appwrite::Exception.new('Missing required parameter: "clientId"')
    end

    if redirect_uri.nil?
      raise Appwrite::Exception.new('Missing required parameter: "redirectUri"')
    end

    if response_type.nil?
      raise Appwrite::Exception.new('Missing required parameter: "responseType"')
    end

    api_params = {
        client_id: client_id,
        redirect_uri: redirect_uri,
        response_type: response_type,
        scope: scope,
        state: state,
        nonce: nonce,
        code_challenge: code_challenge,
        code_challenge_method: code_challenge_method,
        prompt: prompt,
        max_age: max_age,
        authorization_details: authorization_details,
        resource: resource,
        audience: audience,
    }
    
    api_headers = {
        "content-type": 'application/json',
        "accept": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2PAR
    )

end

#create_token(grant_type:, code: nil, refresh_token: nil, device_code: nil, client_id: nil, client_secret: nil, code_verifier: nil, redirect_uri: nil, resource: nil, audience: nil) ⇒ Oauth2Token

Exchange an OAuth2 authorization code, refresh token, or device code for access and refresh tokens.

Parameters:

  • grant_type (String)

    OAuth2 grant type. Can be one of: authorization_code, refresh_token, urn:ietf:params:oauth:grant-type:device_code.

  • code (String) (defaults to: nil)

    Authorization code to be exchanged for access and refresh tokens. Required for authorization_code grant type.

  • refresh_token (String) (defaults to: nil)

    Refresh token to be exchanged for a new access and refresh tokens. Required for refresh_token grant type.

  • device_code (String) (defaults to: nil)

    Device code obtained from the device authorization endpoint. Required for urn:ietf:params:oauth:grant-type:device_code grant type.

  • client_id (String) (defaults to: nil)

    OAuth2 client ID. Either a registered app ID or an HTTPS client ID metadata document URL.

  • client_secret (String) (defaults to: nil)

    OAuth2 client secret. Required for confidential apps.

  • code_verifier (String) (defaults to: nil)

    PKCE code verifier. Required for public apps.

  • redirect_uri (String) (defaults to: nil)

    Redirect URI. Required for authorization_code grant type.

  • resource (String) (defaults to: nil)

    RFC 8707 resource indicator URI or URI list. Each value must be an absolute URI without a fragment.

  • audience (String) (defaults to: nil)

    Compatibility alias for a single OAuth2 resource indicator URI.

Returns:

  • (Oauth2Token)


488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/appwrite/services/oauth2.rb', line 488

def create_token(grant_type:, code: nil, refresh_token: nil, device_code: nil, client_id: nil, client_secret: nil, code_verifier: nil, redirect_uri: nil, resource: nil, audience: nil)
    api_path = '/oauth2/{project_id}/token'
        .gsub('{project_id}', @client.get_config('project'))

    if grant_type.nil?
      raise Appwrite::Exception.new('Missing required parameter: "grantType"')
    end

    api_params = {
        grant_type: grant_type,
        code: code,
        refresh_token: refresh_token,
        device_code: device_code,
        client_id: client_id,
        client_secret: client_secret,
        code_verifier: code_verifier,
        redirect_uri: redirect_uri,
        resource: resource,
        audience: audience,
    }
    
    api_headers = {
        "content-type": 'application/json',
        "accept": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2Token
    )

end

#get_grant(grant_id:) ⇒ Oauth2Grant

Get an OAuth2 grant by its ID. Used by the consent screen to display the details of the authorization the user is being asked to approve. A grant can only be read by the user it belongs to, or by server SDK.

Parameters:

  • grant_id (String)

    Grant ID made during authorization, provided to consent screen in URL search params.

Returns:

  • (Oauth2Grant)


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/appwrite/services/oauth2.rb', line 244

def get_grant(grant_id:)
    api_path = '/oauth2/{project_id}/grants/{grant_id}'
        .gsub('{project_id}', @client.get_config('project'))
        .gsub('{grant_id}', grant_id)

    if grant_id.nil?
      raise Appwrite::Exception.new('Missing required parameter: "grantId"')
    end

    api_params = {
    }
    
    api_headers = {
        "accept": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2Grant
    )

end

#list_organizations(limit: nil, offset: nil, search: nil) ⇒ Oauth2OrganizationList

List the organizations the OAuth2 access token can access. Resolves the token's organization authorization details, expanding the * wildcard into the concrete set of organizations the user can see.

Parameters:

  • limit (Integer) (defaults to: nil)

    Maximum number of organizations to return. Between 1 and 5000.

  • offset (Integer) (defaults to: nil)

    Number of organizations to skip before returning results. Used for pagination.

  • search (String) (defaults to: nil)

    Search term to filter your list results. Max length: 256 chars.

Returns:

  • (Oauth2OrganizationList)


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/appwrite/services/oauth2.rb', line 279

def list_organizations(limit: nil, offset: nil, search: nil)
    api_path = '/oauth2/{project_id}/organizations'
        .gsub('{project_id}', @client.get_config('project'))

    api_params = {
        limit: limit,
        offset: offset,
        search: search,
    }
    
    api_headers = {
        "accept": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2OrganizationList
    )

end

#list_projects(limit: nil, offset: nil, search: nil) ⇒ Oauth2ProjectList

List the projects the OAuth2 access token can access. Resolves the token's project authorization details, expanding the * wildcard into the concrete set of projects the user can see.

Parameters:

  • limit (Integer) (defaults to: nil)

    Maximum number of projects to return. Between 1 and 5000.

  • offset (Integer) (defaults to: nil)

    Number of projects to skip before returning results. Used for pagination.

  • search (String) (defaults to: nil)

    Search term to filter your list results. Max length: 256 chars.

Returns:

  • (Oauth2ProjectList)


377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/appwrite/services/oauth2.rb', line 377

def list_projects(limit: nil, offset: nil, search: nil)
    api_path = '/oauth2/{project_id}/projects'
        .gsub('{project_id}', @client.get_config('project'))

    api_params = {
        limit: limit,
        offset: offset,
        search: search,
    }
    
    api_headers = {
        "accept": 'application/json',
    }

    @client.call(
        method: 'GET',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2ProjectList
    )

end

#reject(grant_id:) ⇒ Oauth2Reject

Reject an OAuth2 grant when the user denies consent. Returns the redirectUrl the end user should be sent to with an access_denied error. You can pass Accept header of application/json to receive a JSON response instead of a redirect.

Parameters:

  • grant_id (String)

    Grant ID made during authorization, provided to consent screen in URL search params.

Returns:

  • (Oauth2Reject)


409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/appwrite/services/oauth2.rb', line 409

def reject(grant_id:)
    api_path = '/oauth2/{project_id}/reject'
        .gsub('{project_id}', @client.get_config('project'))

    if grant_id.nil?
      raise Appwrite::Exception.new('Missing required parameter: "grantId"')
    end

    api_params = {
        grant_id: grant_id,
    }
    
    api_headers = {
        "content-type": 'application/json',
        "accept": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: api_path,
        headers: api_headers,
        params: api_params,
        response_type: Models::Oauth2Reject
    )

end

#revoke(token:, token_type_hint: nil, client_id: nil, client_secret: nil) ⇒ Object

Revoke an OAuth2 access token or refresh token.

Parameters:

  • token (String)

    The access or refresh token to revoke.

  • token_type_hint (String) (defaults to: nil)

    Type of token to revoke (access_token or refresh_token).

  • client_id (String) (defaults to: nil)

    OAuth2 client ID. Either a registered app ID or an HTTPS client ID metadata document URL.

  • client_secret (String) (defaults to: nil)

    OAuth2 client secret. Required for confidential apps; omitted for public apps.

Returns:

  • []



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/appwrite/services/oauth2.rb', line 444

def revoke(token:, token_type_hint: nil, client_id: nil, client_secret: nil)
    api_path = '/oauth2/{project_id}/revoke'
        .gsub('{project_id}', @client.get_config('project'))

    if token.nil?
      raise Appwrite::Exception.new('Missing required parameter: "token"')
    end

    api_params = {
        token: token,
        token_type_hint: token_type_hint,
        client_id: client_id,
        client_secret: client_secret,
    }
    
    api_headers = {
        "content-type": 'application/json',
        "accept": 'application/json',
    }

    @client.call(
        method: 'POST',
        path: api_path,
        headers: api_headers,
        params: api_params,
    )

end