# Copyright (C) 2018 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . import qvisqve class AuthorizationAttempt: def __init__(self): self._attempt_id = None self._client_id = None self._subject_id = None self._state = None self._redirect_uri = None self._scope = None self._authorization_code = None def set_client_id(self, client_id): self._client_id = client_id def get_client_id(self): return self._client_id def set_subject_id(self, subject_id): self._subject_id = subject_id def get_subject_id(self): return self._subject_id def set_state(self, state): self._state = state def get_state(self): return self._state def set_redirect_uri(self, uri): self._redirect_uri = uri def get_redirect_uri(self): return self._redirect_uri def set_scope(self, scope): self._scope = scope def get_scope(self): return self._scope def set_attempt_id(self, attempt_id): required = [ '_client_id', '_state', '_redirect_uri', '_scope', ] for attr in required: if getattr(self, attr, None) is None: raise AuthorizationAttemptError() self._attempt_id = attempt_id def get_attempt_id(self): return self._attempt_id def set_authorization_code(self, authorization_code): self._authorization_code = authorization_code def get_authorization_code(self): return self._authorization_code class AuthorizationAttemptError(Exception): pass class AuthorizationAttempts: def __init__(self): self._attempts = [] def create_attempt(self, urlparams): gen = qvisqve.NonceGenerator() attempt_id = gen.create_nonce() aa = AuthorizationAttempt() aa.set_client_id(urlparams['client_id']) aa.set_state(urlparams['state']) aa.set_redirect_uri(urlparams['redirect_uri']) aa.set_scope(urlparams['scope']) aa.set_attempt_id(attempt_id) self._attempts.append(aa) return aa def delete_by_id(self, attempt_id): self._attempts = [ aa for aa in self._attempts if aa.get_attempt_id() != attempt_id ] def find_by_id(self, attempt_id): for aa in self._attempts: if aa.get_attempt_id() == attempt_id: return aa return None def find_by_code(self, code): for aa in self._attempts: if aa.get_authorization_code() == code: return aa return None