def test_auth_response_token_encoding(self): # without username, testing basics auth_response = AuthResponse(self.private_key_hex, RYAN_PROFILE) auth_response_token = auth_response.token() decoded_token = AuthResponse.decode(auth_response_token) payload = decoded_token['payload'] self.assertEqual(payload['public_keys'][0], self.public_key_hex) self.assertEqual(get_address_from_did(payload['iss']), self.public_key.address()) self.assertEqual(payload['profile'], self.profile) self.assertEqual(payload['username'], None) self.assertTrue(AuthResponse.verify(auth_response_token)) # with username with requests_mock.mock() as m: m.get(LOCALHOST_CORE_API + NAME_LOOKUP_URL + self.username, text=json.dumps({'address': self.public_key.address()})) auth_response = AuthResponse(self.private_key_hex, RYAN_PROFILE, self.username) auth_response_token = auth_response.token() self.assertTrue( do_public_keys_match_username( auth_response_token, Tokenizer(), AuthResponse.decode(auth_response_token))) self.assertTrue(AuthResponse.verify(auth_response_token))
def test_anon_auth_response_encoding(self): challenge = str(uuid.uuid4()) auth_response = AuthResponse( self.private_key_hex, self.public_key_hex, self.challenge) auth_response_token = auth_response.token() is_valid_token = AuthResponse.verify(auth_response_token, self.resolver) self.assertTrue(is_valid_token)
def test_anon_auth_response_encoding(self): challenge = str(uuid.uuid4()) auth_response = AuthResponse(self.private_key_hex, self.public_key_hex, self.challenge) auth_response_token = auth_response.token() is_valid_token = AuthResponse.verify(auth_response_token, self.resolver) self.assertTrue(is_valid_token)
def test_auth_response_token_encoding(self): challenge = str(uuid.uuid4()) auth_response = AuthResponse( self.private_key.to_pem(), self.private_key.public_key().to_hex(), self.challenge, self.blockchainid, self.master_public_key, self.chain_path) auth_response_token = auth_response.token() is_valid_token = AuthResponse.verify(auth_response_token, self.resolver) self.assertTrue(is_valid_token)
def test_auth_response_token_encoding(self): challenge = str(uuid.uuid4()) auth_response = AuthResponse( self.private_key_hex, self.public_key_hex, self.challenge, blockchain_id=self.blockchainid, public_keychain=self.public_keychain, chain_path=self.chain_path) auth_response_token = auth_response.token() is_valid_token = AuthResponse.verify(auth_response_token, self.resolver) self.assertTrue(is_valid_token)
def test_auth_response_token_encoding(self): challenge = str(uuid.uuid4()) auth_response = AuthResponse(self.private_key_hex, self.public_key_hex, self.challenge, blockchain_id=self.blockchainid, public_keychain=self.public_keychain, chain_path=self.chain_path) auth_response_token = auth_response.token() is_valid_token = AuthResponse.verify(auth_response_token, self.resolver) self.assertTrue(is_valid_token)
def authenticate(self, request, auth_response_token=None): try: verified = AuthResponse.verify(auth_response_token) except (ValueError, DecodeError): return None if verified: username = AuthResponse.decode( auth_response_token)['payload']['username'] user, _ = User.objects.get_or_create(username=username) try: profile = fetch_profile(username) except: pass else: claim = profile.get('claim', {}) name = claim.get('name', None) if name: name = name.split(' ', 1) user.first_name = name[0] if len(name) == 2: user.last_name = name[1] user.save() return user
def get(self, request): next_url = request.GET.get('next') ar = request.GET.get('authResponse', u'') try: response = AuthResponse.decode(ar) except DecodeError: return HttpResponseBadRequest('Bad authentication response') payload = response['payload'] user = utils.get_or_create_user(payload) login(self.request, user) return HttpResponseRedirect(next_url)
def test_auth_response_token_decoding(self): decoded_token = AuthResponse.decode(self.sample_encoded_token) decoded_token_payload = decoded_token['payload'] self.assertEqual(decoded_token_payload, self.sample_decoded_token_payload)
def test_auth_response_token_decoding(self): decoded_token = AuthResponse.decode(self.sample_encoded_token) self.assertEqual(decoded_token, self.sample_decoded_token)