-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
218 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from __future__ import annotations | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from vonage import Client | ||
|
||
|
||
class CamaraAuth: | ||
"""Class containing methods for authenticating APIs following Camara standards.""" | ||
|
||
def __init__(self, client: Client): | ||
self._client = client | ||
self._auth_type = 'jwt' | ||
|
||
def make_oidc_request(self, number: str, scope: str): | ||
"""Make an OIDC request to authenticate a user. | ||
Returns a code that can be used to request a Camara token.""" | ||
|
||
login_hint = f'tel:+{number}' | ||
params = {'login_hint': login_hint, 'scope': scope} | ||
|
||
return self._client.post( | ||
'api-eu.vonage.com', | ||
'/oauth2/bc-authorize', | ||
params=params, | ||
auth_type=self._auth_type, | ||
body_is_json=False, | ||
) | ||
|
||
def request_camara_token( | ||
self, oidc_response: dict, grant_type: str = 'urn:openid:params:grant-type:ciba' | ||
): | ||
"""Request a Camara token using an authentication request ID given as a | ||
response to the OIDC request. | ||
""" | ||
params = { | ||
'grant_type': grant_type, | ||
'auth_req_id': oidc_response['auth_req_id'], | ||
} | ||
token_response = self._client.post( | ||
'api-eu.vonage.com', | ||
'/oauth2/token', | ||
params=params, | ||
auth_type=self._auth_type, | ||
) | ||
return token_response['access_token'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"auth_req_id": "0dadaeb4-7c79-4d39-b4b0-5a6cc08bf537", | ||
"expires_in": "120", | ||
"interval": "2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZWZhdWx0IiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.3wWzV6t8bFJUZ6k0WJ9kY3J2kNw9v5zXJ8x1J5g1v2k", | ||
"token_type": "A-VALID-TOKEN-TYPE", | ||
"refresh_token": "A-VALID-REFRESH-TOKEN" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"swapped": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"latestSimChange": "2019-08-24T14:15:22Z" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from vonage.camara_auth import CamaraAuth | ||
from util import * | ||
|
||
import responses | ||
|
||
|
||
@responses.activate | ||
def test_oidc_request(camara_auth: CamaraAuth): | ||
stub( | ||
responses.POST, | ||
'https://api-eu.vonage.com/oauth2/bc-authorize', | ||
fixture_path='camara_auth/oidc_request.json', | ||
) | ||
|
||
response = camara_auth.make_oidc_request( | ||
number='447700900000', | ||
scope='dpv:FraudPreventionAndDetection#check-sim-swap', | ||
) | ||
|
||
assert response['auth_req_id'] == '0dadaeb4-7c79-4d39-b4b0-5a6cc08bf537' | ||
assert response['expires_in'] == '120' | ||
assert response['interval'] == '2' | ||
|
||
|
||
@responses.activate | ||
def test_request_camara_access_token(camara_auth: CamaraAuth): | ||
stub( | ||
responses.POST, | ||
'https://api-eu.vonage.com/oauth2/token', | ||
fixture_path='camara_auth/token_request.json', | ||
) | ||
|
||
oidc_response = { | ||
'auth_req_id': '0dadaeb4-7c79-4d39-b4b0-5a6cc08bf537', | ||
'expires_in': '120', | ||
'interval': '2', | ||
} | ||
response = camara_auth.request_camara_token(oidc_response) | ||
|
||
assert ( | ||
response | ||
== 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZWZhdWx0IiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.3wWzV6t8bFJUZ6k0WJ9kY3J2kNw9v5zXJ8x1J5g1v2k' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from vonage.sim_swap import SimSwap | ||
from util import * | ||
|
||
import responses | ||
|
||
|
||
@responses.activate | ||
def test_check_sim_swap(sim_swap: SimSwap): | ||
stub( | ||
responses.POST, | ||
'https://api-eu.vonage.com/oauth2/bc-authorize', | ||
fixture_path='camara_auth/oidc_request.json', | ||
) | ||
stub( | ||
responses.POST, | ||
'https://api-eu.vonage.com/oauth2/token', | ||
fixture_path='camara_auth/token_request.json', | ||
) | ||
stub( | ||
responses.POST, | ||
'https://api-eu.vonage.com/camara/sim-swap/v040/check', | ||
fixture_path='sim_swap/check_sim_swap.json', | ||
) | ||
|
||
response = sim_swap.check('447700900000', max_age=24) | ||
|
||
assert response['swapped'] == True | ||
|
||
|
||
@responses.activate | ||
def test_get_last_swap_date(sim_swap: SimSwap): | ||
stub( | ||
responses.POST, | ||
'https://api-eu.vonage.com/oauth2/bc-authorize', | ||
fixture_path='camara_auth/oidc_request.json', | ||
) | ||
stub( | ||
responses.POST, | ||
'https://api-eu.vonage.com/oauth2/token', | ||
fixture_path='camara_auth/token_request.json', | ||
) | ||
stub( | ||
responses.POST, | ||
'https://api-eu.vonage.com/camara/sim-swap/v040/retrieve-date', | ||
fixture_path='sim_swap/get_swap_date.json', | ||
) | ||
|
||
response = sim_swap.get_last_swap_date('447700900000') | ||
|
||
assert response['latestSimChange'] == '2019-08-24T14:15:22Z' |