1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
17 # For those usages not covered by the Apache License, Version 2.0 please
18 # contact: esousa@whitestack.com or glavado@whitestack.com
22 Authconn implements an Abstract class for the Auth backend connector
23 plugins with the definition of the methods to be implemented.
26 __author__
= "Eduardo Sousa <esousa@whitestack.com>"
27 __date__
= "$27-jul-2018 23:59:59$"
29 from http
import HTTPStatus
32 class AuthException(Exception):
36 def __init__(self
, message
, http_code
=HTTPStatus
.UNAUTHORIZED
):
37 super(AuthException
, self
).__init
__(message
)
38 self
.http_code
= http_code
41 class AuthconnException(Exception):
43 Common and base class Exception for all authconn exceptions.
45 def __init__(self
, message
, http_code
=HTTPStatus
.UNAUTHORIZED
):
46 super(AuthconnException
, self
).__init
__(message
)
47 self
.http_code
= http_code
50 class AuthconnConnectionException(AuthconnException
):
52 Connectivity error with Auth backend.
54 def __init__(self
, message
, http_code
=HTTPStatus
.BAD_GATEWAY
):
55 super(AuthconnConnectionException
, self
).__init
__(message
, http_code
)
58 class AuthconnNotSupportedException(AuthconnException
):
60 The request is not supported by the Auth backend.
62 def __init__(self
, message
, http_code
=HTTPStatus
.NOT_IMPLEMENTED
):
63 super(AuthconnNotSupportedException
, self
).__init
__(message
, http_code
)
66 class AuthconnNotImplementedException(AuthconnException
):
68 The method is not implemented by the Auth backend.
70 def __init__(self
, message
, http_code
=HTTPStatus
.NOT_IMPLEMENTED
):
71 super(AuthconnNotImplementedException
, self
).__init
__(message
, http_code
)
74 class AuthconnOperationException(AuthconnException
):
76 The operation executed failed.
78 def __init__(self
, message
, http_code
=HTTPStatus
.INTERNAL_SERVER_ERROR
):
79 super(AuthconnOperationException
, self
).__init
__(message
, http_code
)
84 Abstract base class for all the Auth backend connector plugins.
85 Each Auth backend connector plugin must be a subclass of
88 def __init__(self
, config
):
90 Constructor of the Authconn class.
94 :param config: configuration dictionary containing all the
95 necessary configuration parameters.
99 def authenticate_with_user_password(self
, user
, password
):
101 Authenticate a user using username and password.
103 :param user: username
104 :param password: password
105 :return: an unscoped token that grants access to project list
107 raise AuthconnNotImplementedException("Should have implemented this")
109 def authenticate_with_token(self
, token
, project
=None):
111 Authenticate a user using a token. Can be used to revalidate the token
112 or to get a scoped token.
114 :param token: a valid token.
115 :param project: (optional) project for a scoped token.
116 :return: return a revalidated token, scoped if a project was passed or
117 the previous token was already scoped.
119 raise AuthconnNotImplementedException("Should have implemented this")
121 def validate_token(self
, token
):
123 Check if the token is valid.
125 :param token: token to validate
126 :return: dictionary with information associated with the token. If the
127 token is not valid, returns None.
129 raise AuthconnNotImplementedException("Should have implemented this")
131 def revoke_token(self
, token
):
135 :param token: token to be revoked
137 raise AuthconnNotImplementedException("Should have implemented this")
139 def get_user_project_list(self
, token
):
141 Get all the projects associated with a user.
143 :param token: valid token
144 :return: list of projects
146 raise AuthconnNotImplementedException("Should have implemented this")
148 def get_user_role_list(self
, token
):
150 Get role list for a scoped project.
152 :param token: scoped token.
153 :return: returns the list of roles for the user in that project. If
154 the token is unscoped it returns None.
156 raise AuthconnNotImplementedException("Should have implemented this")
158 def create_user(self
, user
, password
):
162 :param user: username.
163 :param password: password.
164 :raises AuthconnOperationException: if user creation failed.
166 raise AuthconnNotImplementedException("Should have implemented this")
168 def change_password(self
, user
, new_password
):
170 Change the user password.
172 :param user: username.
173 :param new_password: new password.
174 :raises AuthconnOperationException: if user password change failed.
176 raise AuthconnNotImplementedException("Should have implemented this")
178 def delete_user(self
, user_id
):
182 :param user_id: user identifier.
183 :raises AuthconnOperationException: if user deletion failed.
185 raise AuthconnNotImplementedException("Should have implemented this")
187 def get_user_list(self
, filter_q
={}):
191 :param filter_q: dictionary to filter user list.
192 :return: returns a list of users.
195 def create_role(self
, role
):
199 :param role: role name.
200 :raises AuthconnOperationException: if role creation failed.
202 raise AuthconnNotImplementedException("Should have implemented this")
204 def delete_role(self
, role_id
):
208 :param role_id: role identifier.
209 :raises AuthconnOperationException: if user deletion failed.
211 raise AuthconnNotImplementedException("Should have implemented this")
213 def get_role_list(self
):
217 :return: list of roles
219 raise AuthconnNotImplementedException("Should have implemented this")
221 def create_project(self
, project
):
225 :param project: project name.
226 :return: the internal id of the created project
227 :raises AuthconnOperationException: if project creation failed.
229 raise AuthconnNotImplementedException("Should have implemented this")
231 def delete_project(self
, project_id
):
235 :param project_id: project identifier.
236 :raises AuthconnOperationException: if project deletion failed.
238 raise AuthconnNotImplementedException("Should have implemented this")
240 def get_project_list(self
, filter_q
={}):
242 Get all the projects.
244 :param filter_q: dictionary to filter project list.
245 :return: list of projects
247 raise AuthconnNotImplementedException("Should have implemented this")
249 def update_project(self
, project_id
, new_name
):
251 Change the name of a project
252 :param project_id: project to be changed
253 :param new_name: new name
256 raise AuthconnNotImplementedException("Should have implemented this")
258 def assign_role_to_user(self
, user
, project
, role
):
260 Assigning a role to a user in a project.
262 :param user: username.
263 :param project: project name.
264 :param role: role name.
265 :raises AuthconnOperationException: if role assignment failed.
267 raise AuthconnNotImplementedException("Should have implemented this")
269 def remove_role_from_user(self
, user
, project
, role
):
271 Remove a role from a user in a project.
273 :param user: username.
274 :param project: project name.
275 :param role: role name.
276 :raises AuthconnOperationException: if role assignment revocation failed.
278 raise AuthconnNotImplementedException("Should have implemented this")