Skip to content

auth

Auth

Bases: Resource

Source code in nylas/resources/auth.py
 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
class Auth(Resource):
    @property
    def grants(self) -> Grants:
        """
        Access the Grants API.

        Returns:
            The Grants API.
        """
        return Grants(self._http_client)

    def url_for_oauth2(self, config: URLForAuthenticationConfig) -> str:
        """
        Build the URL for authenticating users to your application via Hosted Authentication.

        Args:
            config: The configuration for building the URL.

        Returns:
            The URL for hosted authentication.
        """
        query = _build_query(config)

        return self._url_auth_builder(query)

    def exchange_code_for_token(
        self, request: CodeExchangeRequest
    ) -> CodeExchangeResponse:
        """
        Exchange an authorization code for an access token.

        Args:
            request: The request parameters for the code exchange

        Returns:
            Information about the Nylas application
        """

        request_body = dict(request)
        request_body["grant_type"] = "authorization_code"

        return self._get_token(request_body)

    def refresh_access_token(
        self, request: TokenExchangeRequest
    ) -> CodeExchangeResponse:
        """
        Refresh an access token.

        Args:
            request: The refresh token request.

        Returns:
            The response containing the new access token.
        """

        request_body = dict(request)
        request_body["grant_type"] = "refresh_token"

        return self._get_token(request_body)

    def id_token_info(self, id_token: str) -> TokenInfoResponse:
        """
        Get info about an ID token.

        Args:
            id_token: The ID token to query.

        Returns:
            The API response with the token information.
        """

        query_params = {
            "id_token": id_token,
        }

        return self._get_token_info(query_params)

    def validate_access_token(self, access_token: str) -> TokenInfoResponse:
        """
        Get info about an access token.

        Args:
            access_token: The access token to query.

        Returns:
            The API response with the token information.
        """

        query_params = {
            "access_token": access_token,
        }

        return self._get_token_info(query_params)

    def url_for_oauth2_pkce(self, config: URLForAuthenticationConfig) -> PkceAuthUrl:
        """
        Build the URL for authenticating users to your application via Hosted Authentication with PKCE.

        IMPORTANT: YOU WILL NEED TO STORE THE 'secret' returned to use it inside the CodeExchange flow

        Args:
            config: The configuration for the authentication request.

        Returns:
            The URL for hosted authentication with secret & hashed secret.
        """
        secret = str(uuid.uuid4())
        secret_hash = _hash_pkce_secret(secret)
        query = _build_query_with_pkce(config, secret_hash)

        return PkceAuthUrl(secret, secret_hash, self._url_auth_builder(query))

    def url_for_admin_consent(self, config: URLForAdminConsentConfig) -> str:
        """Build the URL for admin consent authentication for Microsoft.

        Args:
            config: The configuration for the authentication request.

        Returns:
            The URL for hosted authentication.
        """
        config_with_provider = {"provider": "microsoft", **config}
        query = _build_query_with_admin_consent(config_with_provider)

        return self._url_auth_builder(query)

    def revoke(self, token: str) -> True:
        """Revoke a single access token.

        Args:
            token: The access token to revoke.

        Returns:
            True: If the token was revoked successfully.
        """
        self._http_client._execute(
            method="POST",
            path="/v3/connect/revoke",
            query_params={"token": token},
        )

        return True

    def detect_provider(
        self, params: ProviderDetectParams
    ) -> Response[ProviderDetectResponse]:
        """
        Detect provider from email address.

        Args:
            params: The parameters to include in the request

        Returns:
            The detected provider, if found.
        """

        json_response = self._http_client._execute(
            method="POST",
            path="/v3/providers/detect",
            query_params=params,
        )
        return Response.from_dict(json_response, ProviderDetectResponse)

    def _url_auth_builder(self, query: dict) -> str:
        return "{}/v3/connect/auth?{}".format(
            self._http_client.api_server, urllib.parse.urlencode(query)
        )

    def _get_token(self, request_body: dict) -> CodeExchangeResponse:
        json_response = self._http_client._execute(
            method="POST", path="/v3/connect/token", request_body=request_body
        )
        return CodeExchangeResponse.from_dict(json_response)

    def _get_token_info(self, query_params: dict) -> TokenInfoResponse:
        json_response = self._http_client._execute(
            method="GET", path="/v3/connect/tokeninfo", query_params=query_params
        )
        return TokenInfoResponse.from_dict(json_response)

grants: Grants property

Access the Grants API.

Returns:

Type Description
Grants

The Grants API.

detect_provider(params)

Detect provider from email address.

Parameters:

Name Type Description Default
params ProviderDetectParams

The parameters to include in the request

required

Returns:

Type Description
Response[ProviderDetectResponse]

The detected provider, if found.

Source code in nylas/resources/auth.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
def detect_provider(
    self, params: ProviderDetectParams
) -> Response[ProviderDetectResponse]:
    """
    Detect provider from email address.

    Args:
        params: The parameters to include in the request

    Returns:
        The detected provider, if found.
    """

    json_response = self._http_client._execute(
        method="POST",
        path="/v3/providers/detect",
        query_params=params,
    )
    return Response.from_dict(json_response, ProviderDetectResponse)

exchange_code_for_token(request)

Exchange an authorization code for an access token.

Parameters:

Name Type Description Default
request CodeExchangeRequest

The request parameters for the code exchange

required

Returns:

Type Description
CodeExchangeResponse

Information about the Nylas application

Source code in nylas/resources/auth.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def exchange_code_for_token(
    self, request: CodeExchangeRequest
) -> CodeExchangeResponse:
    """
    Exchange an authorization code for an access token.

    Args:
        request: The request parameters for the code exchange

    Returns:
        Information about the Nylas application
    """

    request_body = dict(request)
    request_body["grant_type"] = "authorization_code"

    return self._get_token(request_body)

id_token_info(id_token)

Get info about an ID token.

Parameters:

Name Type Description Default
id_token str

The ID token to query.

required

Returns:

Type Description
TokenInfoResponse

The API response with the token information.

Source code in nylas/resources/auth.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def id_token_info(self, id_token: str) -> TokenInfoResponse:
    """
    Get info about an ID token.

    Args:
        id_token: The ID token to query.

    Returns:
        The API response with the token information.
    """

    query_params = {
        "id_token": id_token,
    }

    return self._get_token_info(query_params)

refresh_access_token(request)

Refresh an access token.

Parameters:

Name Type Description Default
request TokenExchangeRequest

The refresh token request.

required

Returns:

Type Description
CodeExchangeResponse

The response containing the new access token.

Source code in nylas/resources/auth.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def refresh_access_token(
    self, request: TokenExchangeRequest
) -> CodeExchangeResponse:
    """
    Refresh an access token.

    Args:
        request: The refresh token request.

    Returns:
        The response containing the new access token.
    """

    request_body = dict(request)
    request_body["grant_type"] = "refresh_token"

    return self._get_token(request_body)

revoke(token)

Revoke a single access token.

Parameters:

Name Type Description Default
token str

The access token to revoke.

required

Returns:

Name Type Description
True True

If the token was revoked successfully.

Source code in nylas/resources/auth.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def revoke(self, token: str) -> True:
    """Revoke a single access token.

    Args:
        token: The access token to revoke.

    Returns:
        True: If the token was revoked successfully.
    """
    self._http_client._execute(
        method="POST",
        path="/v3/connect/revoke",
        query_params={"token": token},
    )

    return True

Build the URL for admin consent authentication for Microsoft.

Parameters:

Name Type Description Default
config URLForAdminConsentConfig

The configuration for the authentication request.

required

Returns:

Type Description
str

The URL for hosted authentication.

Source code in nylas/resources/auth.py
172
173
174
175
176
177
178
179
180
181
182
183
184
def url_for_admin_consent(self, config: URLForAdminConsentConfig) -> str:
    """Build the URL for admin consent authentication for Microsoft.

    Args:
        config: The configuration for the authentication request.

    Returns:
        The URL for hosted authentication.
    """
    config_with_provider = {"provider": "microsoft", **config}
    query = _build_query_with_admin_consent(config_with_provider)

    return self._url_auth_builder(query)

url_for_oauth2(config)

Build the URL for authenticating users to your application via Hosted Authentication.

Parameters:

Name Type Description Default
config URLForAuthenticationConfig

The configuration for building the URL.

required

Returns:

Type Description
str

The URL for hosted authentication.

Source code in nylas/resources/auth.py
70
71
72
73
74
75
76
77
78
79
80
81
82
def url_for_oauth2(self, config: URLForAuthenticationConfig) -> str:
    """
    Build the URL for authenticating users to your application via Hosted Authentication.

    Args:
        config: The configuration for building the URL.

    Returns:
        The URL for hosted authentication.
    """
    query = _build_query(config)

    return self._url_auth_builder(query)

url_for_oauth2_pkce(config)

Build the URL for authenticating users to your application via Hosted Authentication with PKCE.

IMPORTANT: YOU WILL NEED TO STORE THE 'secret' returned to use it inside the CodeExchange flow

Parameters:

Name Type Description Default
config URLForAuthenticationConfig

The configuration for the authentication request.

required

Returns:

Type Description
PkceAuthUrl

The URL for hosted authentication with secret & hashed secret.

Source code in nylas/resources/auth.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def url_for_oauth2_pkce(self, config: URLForAuthenticationConfig) -> PkceAuthUrl:
    """
    Build the URL for authenticating users to your application via Hosted Authentication with PKCE.

    IMPORTANT: YOU WILL NEED TO STORE THE 'secret' returned to use it inside the CodeExchange flow

    Args:
        config: The configuration for the authentication request.

    Returns:
        The URL for hosted authentication with secret & hashed secret.
    """
    secret = str(uuid.uuid4())
    secret_hash = _hash_pkce_secret(secret)
    query = _build_query_with_pkce(config, secret_hash)

    return PkceAuthUrl(secret, secret_hash, self._url_auth_builder(query))

validate_access_token(access_token)

Get info about an access token.

Parameters:

Name Type Description Default
access_token str

The access token to query.

required

Returns:

Type Description
TokenInfoResponse

The API response with the token information.

Source code in nylas/resources/auth.py
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def validate_access_token(self, access_token: str) -> TokenInfoResponse:
    """
    Get info about an access token.

    Args:
        access_token: The access token to query.

    Returns:
        The API response with the token information.
    """

    query_params = {
        "access_token": access_token,
    }

    return self._get_token_info(query_params)