4 # Copyright 2017 RIFT.IO Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
19 from .auth_drv
import AuthDriver
20 from keystoneauth1
import session
23 class SessionDriver(object):
25 Authentication session class for openstack
27 def __init__(self
, auth_method
, version
, cert_validate
, logger
= None, **kwargs
):
29 Constructor for class SessionDriver
30 auth_method (string): At this point, only "password" based
31 authentication is supported. See AuthDriver.AuthMethod
33 version (string): Keystone API version
34 cert_validate (boolean): Boolean to indicate if certificate validation is required
35 logger (instance of logging.Logger)
36 kwargs a dictionary of following key/value pairs
38 username (string) : Username
39 password (string) : Password
40 auth_url (string) : Authentication URL
41 tenant_name(string): Tenant Name
42 user_domain_name (string) : User domain name
43 project_domain_name (string): Project domain name
44 region (string) : Region name
49 self
.log
= logging
.getLogger('rwcal.openstack.session')
50 self
.log
.setLevel(logging
.DEBUG
)
54 self
._auth
_url
= kwargs
['auth_url']
56 self
._auth
= AuthDriver(auth_method
, version
, logger
, **kwargs
)
57 self
._sess
= session
.Session(auth
=self
._auth
.auth_handle
,
58 verify
= cert_validate
)
67 Returns a valid Auth-Token
69 if not self
._sess
.auth
.get_auth_state():
70 return self
._sess
.get_token()
72 if self
.will_expire_after():
73 self
._sess
.invalidate()
74 return self
._sess
.get_token()
76 return self
._sess
.get_token()
81 def invalidate_auth_token(self
):
83 This method will return a fresh token (in case of HTTP 401 response)
85 self
._sess
.invalidate()
88 def auth_header(self
):
89 return self
._sess
.auth
.get_headers(self
._sess
)
93 return self
._sess
.get_project_id()
97 return self
._sess
.get_user_id()
99 def get_auth_state(self
):
100 return self
._sess
.auth
.get_auth_state()
102 def will_expire_after(self
, timeout
=180):
103 return self
._sess
.auth
.auth_ref
.will_expire_soon(stale_duration
=timeout
)