def token(args): """ Generate an OAuth bearer token for the specified principal. This token is suitable for authenticating HTTP requests to the h API. For example, to authorize yourself as user seanh to your local dev instance of h do: hypothesis token --base 'http://*****:*****@localhost' conf/development.ini Then copy the output and pass it to the h API as the value of an X-Annotator-Auth-Token header. """ from h.auth import get_client, generate_signed_token request = bootstrap(args) registry = request.registry request.client = get_client(request, registry.settings['h.client_id']) request.user = args.sub request.expires_in = args.ttl request.extra_credentials = {} token = generate_signed_token(request) print(token)
def test_get_client_missing(config): mock_factory = MagicMock() mock_factory.return_value = None registry = config.registry registry.registerUtility(mock_factory, interfaces.IClientFactory) request = testing.DummyRequest(registry=config.registry) assert auth.get_client(request, '9876') is None mock_factory.assert_called_with(request, '9876')
def test_get_client(config): client = MockClient(None, '4321') mock_factory = MagicMock() mock_factory.return_value = client registry = config.registry registry.registerUtility(mock_factory, interfaces.IClientFactory) request = testing.DummyRequest(registry=config.registry) assert auth.get_client(request, '4321') is client mock_factory.assert_called_with(request, '4321')
def test_get_client_bad_secret(config): client = MockClient(None, '9876') client.client_secret = 'scramble' mock_factory = MagicMock() mock_factory.return_value = client registry = config.registry registry.registerUtility(mock_factory, interfaces.IClientFactory) request = testing.DummyRequest(registry=config.registry) assert auth.get_client(request, '9876', client_secret='1234') is None mock_factory.assert_called_with(request, '9876')
def token(args): """Generate an OAuth bearer token for the specified principal.""" from h.auth import get_client, generate_signed_token if not args.config_uri.endswith('#api'): args.config_uri += '#api' paster.setup_logging(args.config_uri) env = paster.bootstrap(args.config_uri) request = env['request'] registry = env['registry'] request.client = get_client(request, registry.settings['h.client_id']) request.user = args.sub request.expires_in = args.ttl request.extra_credentials = {} token = generate_signed_token(request) print(token)