Class: Nylas::Auth

Inherits:
Resource show all
Includes:
ApiOperations::Get, ApiOperations::Post
Defined in:
lib/nylas/resources/auth.rb

Overview

Auth

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Nylas::Resource

Instance Method Details

#custom_authentication(request_body) ⇒ Array(Hash, String)

Create a Grant via Custom Authentication.

Parameters:

  • request_body (Hash)

    The values to create the Grant with.

Returns:

  • (Array(Hash, String))

    Created grant and API Request ID.



41
42
43
44
45
46
# File 'lib/nylas/resources/auth.rb', line 41

def custom_authentication(request_body)
  post(
    path: "#{api_uri}/v3/connect/custom",
    request_body: request_body
  )
end

#exchange_code_for_token(request) ⇒ Hash

Exchanges an authorization code for an access token.

Parameters:

  • request (Hash)

    Code exchange request.

Returns:

  • (Hash)

    Token object.



31
32
33
34
35
# File 'lib/nylas/resources/auth.rb', line 31

def exchange_code_for_token(request)
  request[:grant_type] = "authorization_code"

  execute_token_request(request)
end

#refresh_access_token(request) ⇒ Hash

Refreshes an access token.

Parameters:

  • request (Hash)

    Code exchange request.

Returns:

  • (Hash)

    Refreshed token object.



52
53
54
55
56
# File 'lib/nylas/resources/auth.rb', line 52

def refresh_access_token(request)
  request[:grant_type] = "refresh_token"

  execute_token_request(request)
end

#revoke(token) ⇒ Boolean

Revokes a single access token.

Parameters:

  • token (String)

    Access token to revoke.

Returns:

  • (Boolean)

    True if the access token was revoked successfully.



95
96
97
98
99
100
101
102
103
# File 'lib/nylas/resources/auth.rb', line 95

def revoke(token)
  post(
    path: "#{api_uri}/v3/connect/revoke",
    query_params: {
      token: token
    }
  )
  true
end

Builds the URL for admin consent authentication for Microsoft.

Parameters:

  • config (Hash)

    Configuration for the authentication request.

Returns:

  • (String)

    URL for hosted authentication.



81
82
83
84
85
86
87
88
89
# File 'lib/nylas/resources/auth.rb', line 81

def url_for_admin_consent(config)
  config_with_provider = config.merge("provider" => "microsoft")
  url = url_auth_builder(config_with_provider)

  query_params = build_query_with_admin_consent(config)
  url.query = URI.encode_www_form(query_params)

  url.to_s
end

#url_for_oauth2(config) ⇒ String

Builds the URL for authenticating users to your application with OAuth 2.0.

Parameters:

  • config (Hash)

    Configuration for building the URL.

Returns:

  • (String)

    URL for hosted authentication.



23
24
25
# File 'lib/nylas/resources/auth.rb', line 23

def url_for_oauth2(config)
  url_auth_builder(config).to_s
end

#url_for_oauth2_pkce(config) ⇒ OpenStruct

Builds the URL for authenticating users to your application with OAuth 2.0 and PKCE.

IMPORTANT: You must store the 'secret' returned to use it inside the CodeExchange flow.

Parameters:

  • config (Hash)

    Configuration for building the URL.

Returns:

  • (OpenStruct)

    URL for hosted authentication with the secret and the hashed secret.



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nylas/resources/auth.rb', line 63

def url_for_oauth2_pkce(config)
  url = url_auth_builder(config)

  # Generates a secret and hashes it.
  secret = SecureRandom.uuid
  secret_hash = hash_pkce_secret(secret)

  # Adds code challenge to URL generation.
  url.query = build_query_with_pkce(config, secret_hash)

  # Returns the URL with secret and hashed secret.
  OpenStruct.new(secret: secret, secret_hash: secret_hash, url: url.to_s)
end