Skip to content

Latest commit

 

History

History
49 lines (34 loc) · 3.48 KB

File metadata and controls

49 lines (34 loc) · 3.48 KB

Authorization Code Flow + PKCE Python Sample and Test

Version: 1.0.21

Build Status

This sample uses the OAuth2/OIDC Authorization Code Flow + PKCE to obtain an access token. See the main Cds Authentication samples page README for more information about this flow.

Developed against Python 3.9.1

Python SDS Library

The Python SDS library, ocs_sample_library_preview, implements this flow inside its ADHClient type using the same Python code used in this sample. This sample is preserved separately to demonstrate that code in an isolated environment, and also to allow injection of a Selenium script for automated testing.

To use the Authorization Code Flow + PKCE flow with the library, pass a client ID for an Cds Authorization Code client, without a client secret, to the constructor. The libary will use this OIDC flow if no secret is passed to the constructor.

Requirements

  • Python 3
  • Web Browser with Javascript enabled
    • Google Chrome Web Driver is required for the automated test
  • Register an Authorization Code Client in CONNECT data services and ensure that the registered client in Cds contains http://localhost:5004/callback.html in the list of RedirectUris
  • Configure the sample using the file appsettings.placeholder.json. Before editing, rename this file to appsettings.json. This repository's .gitignore rules should prevent the file from ever being checked in to any fork or branch, to ensure credentials are not compromised.
  • Replace the placeholders in appsettings.json with your Tenant ID and Client ID. The username and password fields are used for testing and can be left as is.

Running the Sample

  1. Open a command prompt in this folder
  2. Install requirements, using pip install -r requirements.txt
  3. Set up the Configuration section of the appsettings.json file, as described above
  4. Run the sample using python ./program.py
  5. Follow prompts in the browser to log in, if you are already logged in this step will be skipped
  6. When prompted by the browser, return to the command prompt and a token will be logged

Running the Automated Test

  1. Open a command prompt in this folder
  2. Install pytest, using pip install pytest
  3. Install selenium, using pip install selenium
  4. Set up the Configuration section of the appsettings.json file, as described above, if you have not already
  5. Set up the Test section of the appsettings.json file with a username and password for the test to use to log in
  6. Run the test, using python -m pytest ./test.py
  7. Chrome should run in the background and log in, and the test will assert that a token is obtained

For the main Cds Authentication samples page ReadMe
For the main Cds samples page ReadMe
For the main AVEVA samples page ReadMe