forked from sebbacon/vatreturn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hmrc_provider.py
92 lines (80 loc) · 3.5 KB
/
hmrc_provider.py
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
from __future__ import unicode_literals
from flask_dance.consumer import OAuth2ConsumerBlueprint
from flask_dance.consumer.requests import OAuth2Session
from functools import partial
from flask.globals import LocalProxy, _lookup_app_object
try:
from flask import _app_ctx_stack as stack
except ImportError:
from flask import _request_ctx_stack as stack
class HMRCSession(OAuth2Session):
def __init__(self, *args, **kwargs):
super(HMRCSession, self).__init__(*args, **kwargs)
self.headers["ACCEPT"] = "application/vnd.hmrc.1.0+json"
self.headers["Content-Type"] = "application/json"
def make_hmrc_blueprint(
api_host=None,
client_id=None,
client_secret=None,
scope=None,
redirect_url=None,
redirect_to=None,
login_url=None,
authorized_url=None,
session_class=None,
storage=None,
):
"""
Make a blueprint for authenticating with Hmrc using OAuth 2. This requires
a client ID and client secret from Hmrc. You should either pass them to
this constructor, or make sure that your Flask application config defines
them, using the variables HMRC_OAUTH_CLIENT_ID and HMRC_OAUTH_CLIENT_SECRET.
Args:
client_id (str): The client ID for your application on Hmrc.
client_secret (str): The client secret for your application on Hmrc
scope (str, optional): comma-separated list of scopes for the OAuth token
redirect_url (str): the URL to redirect to after the authentication
dance is complete
redirect_to (str): if ``redirect_url`` is not defined, the name of the
view to redirect to after the authentication dance is complete.
The actual URL will be determined by :func:`flask.url_for`
login_url (str, optional): the URL path for the ``login`` view.
Defaults to ``/hmrc``
authorized_url (str, optional): the URL path for the ``authorized`` view.
Defaults to ``/hmrc/authorized``.
session_class (class, optional): The class to use for creating a
Requests session. Defaults to
:class:`~flask_dance.consumer.requests.OAuth2Session`.
storage: A token storage class, or an instance of a token storage
class, to use for this blueprint. Defaults to
:class:`~flask_dance.consumer.storage.session.SessionStorage`.
:rtype: :class:`~flask_dance.consumer.OAuth2ConsumerBlueprint`
:returns: A :ref:`blueprint <flask:blueprints>` to attach to your Flask app.
"""
hmrc_bp = OAuth2ConsumerBlueprint(
"hmrc",
__name__,
client_id=client_id,
client_secret=client_secret,
scope=scope,
base_url=api_host,
authorization_url=api_host + "/oauth/authorize",
token_url=api_host + "/oauth/token",
auto_refresh_url=api_host + "/oauth/token",
auto_refresh_kwargs={'client_id': client_id, 'client_secret': client_secret},
redirect_url=redirect_url,
redirect_to=redirect_to,
login_url=login_url,
authorized_url=authorized_url,
session_class=session_class or HMRCSession,
storage=storage,
token_url_params={'include_client_id': True}
)
hmrc_bp.from_config["client_id"] = "HMRC_OAUTH_CLIENT_ID"
hmrc_bp.from_config["client_secret"] = "HMRC_OAUTH_CLIENT_SECRET"
@hmrc_bp.before_app_request
def set_applocal_session():
ctx = stack.top
ctx.hmrc_oauth = hmrc_bp.session
return hmrc_bp
hmrc = LocalProxy(partial(_lookup_app_object, "hmrc_oauth"))