|
| 1 | +""" |
| 2 | +OAuth backend for LinkedIn |
| 3 | +""" |
| 4 | +import json |
| 5 | +import logging |
| 6 | +import requests |
| 7 | + |
| 8 | +from oic.utils.authn.authn_context import UNSPECIFIED |
| 9 | +from oic.oauth2.consumer import stateID |
| 10 | +from oic.oauth2.message import AuthorizationResponse |
| 11 | + |
| 12 | +from satosa.backends.oauth import _OAuthBackend |
| 13 | +from ..internal_data import InternalResponse |
| 14 | +from ..internal_data import AuthenticationInformation |
| 15 | +from ..response import Redirect |
| 16 | +from ..util import rndstr |
| 17 | + |
| 18 | + |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class LinkedInBackend(_OAuthBackend): |
| 23 | + """LinkedIn OAuth 2.0 backend""" |
| 24 | + |
| 25 | + def __init__(self, outgoing, internal_attributes, config, base_url, name): |
| 26 | + """LinkedIn backend constructor |
| 27 | + :param outgoing: Callback should be called by the module after the |
| 28 | + authorization in the backend is done. |
| 29 | + :param internal_attributes: Mapping dictionary between SATOSA internal |
| 30 | + attribute names and the names returned by underlying IdP's/OP's as |
| 31 | + well as what attributes the calling SP's and RP's expects namevice. |
| 32 | + :param config: configuration parameters for the module. |
| 33 | + :param base_url: base url of the service |
| 34 | + :param name: name of the plugin |
| 35 | + :type outgoing: |
| 36 | + (satosa.context.Context, satosa.internal_data.InternalResponse) -> |
| 37 | + satosa.response.Response |
| 38 | + :type internal_attributes: dict[string, dict[str, str | list[str]]] |
| 39 | + :type config: dict[str, dict[str, str] | list[str] | str] |
| 40 | + :type base_url: str |
| 41 | + :type name: str |
| 42 | + """ |
| 43 | + config.setdefault('response_type', 'code') |
| 44 | + config['verify_accesstoken_state'] = False |
| 45 | + super().__init__( |
| 46 | + outgoing, internal_attributes, config, base_url, name, 'linkedin', |
| 47 | + 'id') |
| 48 | + |
| 49 | + def start_auth(self, context, internal_request, get_state=stateID): |
| 50 | + """ |
| 51 | + :param get_state: Generates a state to be used in authentication call |
| 52 | +
|
| 53 | + :type get_state: Callable[[str, bytes], str] |
| 54 | + :type context: satosa.context.Context |
| 55 | + :type internal_request: satosa.internal_data.InternalRequest |
| 56 | + :rtype satosa.response.Redirect |
| 57 | + """ |
| 58 | + oauth_state = get_state(self.config["base_url"], rndstr().encode()) |
| 59 | + context.state[self.name] = dict(state=oauth_state) |
| 60 | + |
| 61 | + request_args = dict( |
| 62 | + response_type='code', |
| 63 | + client_id=self.config['client_config']['client_id'], |
| 64 | + redirect_uri=self.redirect_url, |
| 65 | + state=oauth_state) |
| 66 | + scope = ' '.join(self.config['scope']) |
| 67 | + if scope: |
| 68 | + request_args['scope'] = scope |
| 69 | + |
| 70 | + cis = self.consumer.construct_AuthorizationRequest( |
| 71 | + request_args=request_args) |
| 72 | + return Redirect(cis.request(self.consumer.authorization_endpoint)) |
| 73 | + |
| 74 | + def auth_info(self, requrest): |
| 75 | + return AuthenticationInformation( |
| 76 | + UNSPECIFIED, None, |
| 77 | + self.config['server_info']['authorization_endpoint']) |
| 78 | + |
| 79 | + def _authn_response(self, context): |
| 80 | + state_data = context.state[self.name] |
| 81 | + aresp = self.consumer.parse_response( |
| 82 | + AuthorizationResponse, info=json.dumps(context.request)) |
| 83 | + self._verify_state(aresp, state_data, context.state) |
| 84 | + url = self.config['server_info']['token_endpoint'] |
| 85 | + data = dict( |
| 86 | + grant_type='authorization_code', |
| 87 | + code=aresp['code'], |
| 88 | + redirect_uri=self.redirect_url, |
| 89 | + client_id=self.config['client_config']['client_id'], |
| 90 | + client_secret=self.config['client_secret'], ) |
| 91 | + |
| 92 | + r = requests.post(url, data=data) |
| 93 | + response = r.json() |
| 94 | + if self.config.get('verify_accesstoken_state', True): |
| 95 | + self._verify_state(response, state_data, context.state) |
| 96 | + |
| 97 | + user_info = self.user_information(response["access_token"]) |
| 98 | + auth_info = self.auth_info(context.request) |
| 99 | + internal_response = InternalResponse(auth_info=auth_info) |
| 100 | + internal_response.attributes = self.converter.to_internal( |
| 101 | + self.external_type, user_info) |
| 102 | + internal_response.user_id = user_info[self.user_id_attr] |
| 103 | + del context.state[self.name] |
| 104 | + return self.auth_callback_func(context, internal_response) |
| 105 | + |
| 106 | + def user_information(self, access_token): |
| 107 | + url = self.config['server_info']['user_info'] |
| 108 | + headers = {'Authorization': 'Bearer {}'.format(access_token)} |
| 109 | + params = {'format': 'json'} |
| 110 | + r = requests.get(url, params=params, headers=headers) |
| 111 | + return r.json() |
0 commit comments