Skip to content

Commit 45cb34a

Browse files
authored
Merge pull request #117 from ducu/master
Add authorize for system app
2 parents 6047277 + c130888 commit 45cb34a

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

fhirclient/auth.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import uuid
44
import logging
5+
from datetime import datetime, timedelta
56
try: # Python 2.x
67
import urlparse
78
from urllib import urlencode
@@ -143,11 +144,14 @@ def __init__(self, state=None):
143144
self.app_secret = None
144145
self.access_token = None
145146
self.refresh_token = None
147+
self.expires_at = None
146148

147149
super(FHIROAuth2Auth, self).__init__(state=state)
148150

149151
@property
150152
def ready(self):
153+
if self.expires_at and self.expires_at < datetime.now():
154+
self.reset()
151155
return True if self.access_token else False
152156

153157
def reset(self):
@@ -283,6 +287,8 @@ def _request_access_token(self, server, params):
283287
del ret_params['access_token']
284288

285289
if 'expires_in' in ret_params:
290+
expires_in = ret_params.get('expires_in')
291+
self.expires_at = datetime.now() + timedelta(seconds=expires_in)
286292
del ret_params['expires_in']
287293

288294
# The refresh token issued by the authorization server. If present, the
@@ -298,6 +304,29 @@ def _request_access_token(self, server, params):
298304
return ret_params
299305

300306

307+
# MARK: Authorization
308+
309+
def authorize(self, server):
310+
""" Perform authorization on behalf of a system.
311+
312+
:param server: The Server instance to use
313+
"""
314+
logger.debug("SMART AUTH: Get access token")
315+
token_params = self._token_params(server)
316+
return self._request_access_token(server, token_params)
317+
318+
def _token_params(self, server):
319+
""" The URL parameters to use when requesting access token. """
320+
if server is None:
321+
raise Exception("Cannot get token params without server instance")
322+
323+
params = {
324+
'grant_type': 'client_credentials',
325+
'scope': server.desired_scope,
326+
}
327+
return params
328+
329+
301330
# MARK: Reauthorization
302331

303332
def reauthorize(self, server):

fhirclient/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def handle_callback(self, url):
132132
ctx = self.server.handle_callback(url) if self.server is not None else None
133133
self._handle_launch_context(ctx)
134134

135+
def authorize(self):
136+
""" Try to authorize with the server. """
137+
ctx = self.server.authorize() if self.server is not None else None
138+
self._handle_launch_context(ctx)
139+
135140
def reauthorize(self):
136141
""" Try to reauthorize with the server.
137142

fhirclient/server.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ def handle_callback(self, url):
120120
if self.auth is None:
121121
raise Exception("Not ready to handle callback, I do not have an auth instance")
122122
return self.auth.handle_callback(url, self)
123-
123+
124+
def authorize(self):
125+
if self.auth is None:
126+
raise Exception("Not ready to authorize, I do not have an auth instance")
127+
return self.auth.authorize(self) if self.auth is not None else None
128+
124129
def reauthorize(self):
125130
if self.auth is None:
126131
raise Exception("Not ready to reauthorize, I do not have an auth instance")

0 commit comments

Comments
 (0)