| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from oslo_config import cfg |
| 14 |
|
| 15 |
from keystoneclient import access |
| 16 |
from keystoneclient.auth.identity.v3 import federated |
| 17 |
|
| 18 |
|
| 19 |
class OidcPassword(federated.FederatedBaseAuth): |
| 20 |
"""Implement authentication plugin for OpenID Connect protocol. |
| 21 |
|
| 22 |
OIDC or OpenID Connect is a protocol for federated authentication. |
| 23 |
|
| 24 |
The OpenID Connect specification can be found at:: |
| 25 |
``http://openid.net/specs/openid-connect-core-1_0.html`` |
| 26 |
""" |
| 27 |
|
| 28 |
@classmethod |
| 29 |
def get_options(cls): |
| 30 |
options = super(OidcPassword, cls).get_options() |
| 31 |
options.extend([ |
| 32 |
cfg.StrOpt('username', help='Username'), |
| 33 |
cfg.StrOpt('password', secret=True, help='Password'), |
| 34 |
cfg.StrOpt('client-id', help='OAuth 2.0 Client ID'), |
| 35 |
cfg.StrOpt('client-secret', secret=True, |
| 36 |
help='OAuth 2.0 Client Secret'), |
| 37 |
cfg.StrOpt('access-token-endpoint', |
| 38 |
help='OpenID Connect Provider Token Endpoint'), |
| 39 |
cfg.StrOpt('scope', default="profile", |
| 40 |
help='OpenID Connect scope that is requested from OP') |
| 41 |
]) |
| 42 |
return options |
| 43 |
|
| 44 |
def __init__(self, auth_url, identity_provider, protocol, |
| 45 |
username, password, client_id, client_secret, |
| 46 |
access_token_endpoint, scope='profile', |
| 47 |
grant_type='password'): |
| 48 |
"""The OpenID Connect plugin. |
| 49 |
|
| 50 |
It expects the following: |
| 51 |
|
| 52 |
:param auth_url: URL of the Identity Service |
| 53 |
:type auth_url: string |
| 54 |
|
| 55 |
:param identity_provider: Name of the Identity Provider the client |
| 56 |
will authenticate against |
| 57 |
:type identity_provider: string |
| 58 |
|
| 59 |
:param protocol: Protocol name as configured in keystone |
| 60 |
:type protocol: string |
| 61 |
|
| 62 |
:param username: Username used to authenticate |
| 63 |
:type username: string |
| 64 |
|
| 65 |
:param password: Password used to authenticate |
| 66 |
:type password: string |
| 67 |
|
| 68 |
:param client_id: OAuth 2.0 Client ID |
| 69 |
:type client_id: string |
| 70 |
|
| 71 |
:param client_secret: OAuth 2.0 Client Secret |
| 72 |
:type client_secret: string |
| 73 |
|
| 74 |
:param access_token_endpoint: OpenID Connect Provider Token Endpoint, |
| 75 |
for example: |
| 76 |
https://localhost:8020/oidc/OP/token |
| 77 |
:type access_token_endpoint: string |
| 78 |
|
| 79 |
:param scope: OpenID Connect scope that is requested from OP, |
| 80 |
defaults to "profile", for example: "profile email" |
| 81 |
:type scope: string |
| 82 |
|
| 83 |
:param grant_type: OpenID Connect grant type, it represents the flow |
| 84 |
that is used to talk to the OP. Valid values are: |
| 85 |
"authorization_code", "refresh_token", or |
| 86 |
"password". |
| 87 |
:type grant_type: string |
| 88 |
""" |
| 89 |
super(OidcPassword, self).__init__(auth_url, identity_provider, |
| 90 |
protocol) |
| 91 |
self._username = username |
| 92 |
self._password = password |
| 93 |
self.client_id = client_id |
| 94 |
self.client_secret = client_secret |
| 95 |
self.access_token_endpoint = access_token_endpoint |
| 96 |
self.scope = scope |
| 97 |
self.grant_type = grant_type |
| 98 |
|
| 99 |
@property |
| 100 |
def username(self): |
| 101 |
|
| 102 |
return self._username |
| 103 |
|
| 104 |
@username.setter |
| 105 |
def username(self, value): |
| 106 |
|
| 107 |
self._username = value |
| 108 |
|
| 109 |
@property |
| 110 |
def password(self): |
| 111 |
|
| 112 |
return self._password |
| 113 |
|
| 114 |
@password.setter |
| 115 |
def password(self, value): |
| 116 |
|
| 117 |
self._password = value |
| 118 |
|
| 119 |
def get_unscoped_auth_ref(self, session): |
| 120 |
"""Authenticate with OpenID Connect and get back claims. |
| 121 |
|
| 122 |
This is a multi-step process. First an access token must be retrieved, |
| 123 |
to do this, the username and password, the OpenID Connect client ID |
| 124 |
and secret, and the access token endpoint must be known. |
| 125 |
|
| 126 |
Secondly, we then exchange the access token upon accessing the |
| 127 |
protected Keystone endpoint (federated auth URL). This will trigger |
| 128 |
the OpenID Connect Provider to perform a user introspection and |
| 129 |
retrieve information (specified in the scope) about the user in |
| 130 |
the form of an OpenID Connect Claim. These claims will be sent |
| 131 |
to Keystone in the form of environment variables. |
| 132 |
|
| 133 |
:param session: a session object to send out HTTP requests. |
| 134 |
:type session: keystoneclient.session.Session |
| 135 |
|
| 136 |
:returns: a token data representation |
| 137 |
:rtype: :py:class:`keystoneclient.access.AccessInfo` |
| 138 |
""" |
| 139 |
|
| 140 |
client_auth = (self.client_id, self.client_secret) |
| 141 |
payload = {'grant_type': self.grant_type, 'username': self.username, |
| 142 |
'password': self.password, 'scope': self.scope} |
| 143 |
response = self._get_access_token(session, client_auth, payload, |
| 144 |
self.access_token_endpoint) |
| 145 |
access_token = response.json()['access_token'] |
| 146 |
|
| 147 |
|
| 148 |
headers = {'Authorization': 'Bearer ' + access_token} |
| 149 |
response = self._get_keystone_token(session, headers, |
| 150 |
self.federated_token_url) |
| 151 |
|
| 152 |
|
| 153 |
token = response.headers['X-Subject-Token'] |
| 154 |
token_json = response.json()['token'] |
| 155 |
return access.AccessInfoV3(token, **token_json) |
| 156 |
|
| 157 |
def _get_access_token(self, session, client_auth, payload, |
| 158 |
access_token_endpoint): |
| 159 |
"""Exchange a variety of user supplied values for an access token. |
| 160 |
|
| 161 |
:param session: a session object to send out HTTP requests. |
| 162 |
:type session: keystoneclient.session.Session |
| 163 |
|
| 164 |
:param client_auth: a tuple representing client id and secret |
| 165 |
:type client_auth: tuple |
| 166 |
|
| 167 |
:param payload: a dict containing various OpenID Connect values, for |
| 168 |
example:: |
| 169 |
{'grant_type': 'password', 'username': self.username, |
| 170 |
'password': self.password, 'scope': self.scope} |
| 171 |
:type payload: dict |
| 172 |
|
| 173 |
:param access_token_endpoint: URL to use to get an access token, for |
| 174 |
example: https://localhost/oidc/token |
| 175 |
:type access_token_endpoint: string |
| 176 |
""" |
| 177 |
op_response = session.post(self.access_token_endpoint, |
| 178 |
requests_auth=client_auth, |
| 179 |
data=payload, |
| 180 |
authenticated=False) |
| 181 |
return op_response |
| 182 |
|
| 183 |
def _get_keystone_token(self, session, headers, federated_token_url): |
| 184 |
r"""Exchange an acess token for a keystone token. |
| 185 |
|
| 186 |
By Sending the access token in an `Authorization: Bearer` header, to |
| 187 |
an OpenID Connect protected endpoint (Federated Token URL). The |
| 188 |
OpenID Connect server will use the access token to look up information |
| 189 |
about the authenticated user (this technique is called instrospection). |
| 190 |
The output of the instrospection will be an OpenID Connect Claim, that |
| 191 |
will be used against the mapping engine. Should the mapping engine |
| 192 |
succeed, a Keystone token will be presented to the user. |
| 193 |
|
| 194 |
:param session: a session object to send out HTTP requests. |
| 195 |
:type session: keystoneclient.session.Session |
| 196 |
|
| 197 |
:param headers: an Authorization header containing the access token. |
| 198 |
:type headers_: dict |
| 199 |
|
| 200 |
:param federated_auth_url: Protected URL for federated authentication, |
| 201 |
for example: https://localhost:5000/v3/\ |
| 202 |
OS-FEDERATION/identity_providers/bluepages/\ |
| 203 |
protocols/oidc/auth |
| 204 |
:type federated_auth_url: string |
| 205 |
""" |
| 206 |
auth_response = session.post(self.federated_token_url, |
| 207 |
headers=headers, |
| 208 |
authenticated=False) |
| 209 |
return auth_response |