Skip to main content

Pebble client tool for authenticate user and licence management written in Python

Project description

Introduction

This library offer a client for authenticate user and licence management written in Python compatible with may python API Server.

Installation

Requirements

The following procedures explains the installation of the following packages :

  • Python 3.9 or higher

  • pip (provided with Python package)

  • PyJWT (tested with version 2.8.0)

  • cryptography (tested with version 41.0.5)

Solution 1 : requirement.txt configuration

If your project use a requirement.txt configuration file, simply add the following.

requirement.txt file addition:

pebbleauthclient>=0.2.0

Then run this command on your project :

pip install -r requirements.txt

Or in Dockerfile :

RUN pip install -r requirements.txt

Solution 2 : Local installation

Check python3 is properly installed on your local machine (with pip working), then run the following in the application directory.

pip install pebbleauthclient

Or in Dockerfile :

RUN pip install pebbleauthclient

Usage

Configuration

Before you can work with the library, you must define to a system environment variable the URI were is stored the public Json Web Key Set (JWKS file).

This file will be requested and store temporary on your API Server. Your server should be able to write on ./var/credentials/auth/jwks.json . If the file does not exist, it will be created.

If you start your server directly from a terminal, run this command on your terminal before starting your server :

export PBL_JWKS_REMOTE_URI=https://SERVER_URI/path/jwks.json

If you start your server within a Docker container, you should add this line to your Dockefile :

ENV PBL_JWKS_REMOTE_URI=https://SERVER_URI/path/jwks.json

Test keys pair

JWKS URI (for PBL_JWKS_REMOTE_URI environment variable)

https://storage.googleapis.com/pebble-public-cdn/test_auth/jwks_test.json

Public and private keys used to sign a token

https://storage.googleapis.com/pebble-public-cdn/test_auth/public_test.pem

https://storage.googleapis.com/pebble-public-cdn/test_auth/private_test.pem

Authenticate with token string

from pebbleauthclient.auth import auth

try:
    authToken = auth("---A_valid_token---")

    print(authToken)
    print(authToken.get_user())
    print(authToken.get_authenticated_licence())
except Exception as e:
    print("ERROR: " + e)

Authenticate with HTTP Authorization header

from http.server import HTTPServer, BaseHTTPRequestHandler
from pebbleauthclient.auth import auth_from_http_headers


class HandleRequest(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            auth_token = auth_from_http_headers(self.headers)
            licence = auth_token.get_authenticated_licence()
            user = auth_token.get_user()

            print(licence)
            print(user)

            self.send_response(200)
            self.send_header('Content-Type', 'text/plain')
            self.end_headers()

            self.wfile.write(bytes("Welcome " + user.username, 'utf-8'))

        except Exception:
            self.send_response(401)
            self.end_headers()


server = HTTPServer(('', 8084), HandleRequest)
print("Server is waiting...")
server.serve_forever()
server.server_close()

Check the audience

Audience identifies the recipients that the token is intended for. Each resource server MUST be identified by its audience name and the authorization process MUST check that this audience exists in the token.

To check the audience, add an options dictionary to the auth() or auth_from_http_headers() functions.

# Check that the provided token has a valid audience for api.pebble.solutions/v5/my-resource
auth_token = auth("----my.valid.token----", options={
    'audience': "api.pebble.solutions/v5/my-resource"
})

# Check that token communicate through authorization header has a valid audience
# for api.pebble.solutions/v5/my-resource
auth_token = auth_from_http_headers(headers, options={
    'audience': "api.pebble.solutions/v5/my-resource"
})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pebbleauthclient-0.2.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pebbleauthclient-0.2.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file pebbleauthclient-0.2.0.tar.gz.

File metadata

  • Download URL: pebbleauthclient-0.2.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for pebbleauthclient-0.2.0.tar.gz
Algorithm Hash digest
SHA256 362a245037782e3480916e6b0c5ed2cb4530e5f92126a4eff724cc82396626bc
MD5 e0d20059eff51372826af41e29c9b84f
BLAKE2b-256 5dea916992e2dd32800914ccfc87a673c86486662296c25da6ef440e9cfc0a8b

See more details on using hashes here.

File details

Details for the file pebbleauthclient-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pebbleauthclient-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c89a3afa8ebf26e67ba2791b32893f14d6d95fcd0bbb2a855922a945b49fd4ef
MD5 8f38fc7813b586e816cbd024b6d86551
BLAKE2b-256 6a9e549d17109f3962436be4cd0d58f9fbac2d81ab4a226adc523d1ce46f32b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page