Skip to content

Commit 2bde679

Browse files
authored
Merge pull request #1296 from oracle-devrel/ios-iam-py-sdk
Ios iam py sdk
2 parents f2ce063 + 99a4c35 commit 2bde679

File tree

22 files changed

+2441
-0
lines changed

22 files changed

+2441
-0
lines changed

security/security-design/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ Reviewed: 29.10.2024
4343
- [OCI Security Health Check Standard](shared-assets/oci-security-health-check-standard/README.md)
4444
- [Data Safe Audit Database to OCI Logging](shared-assets/fn-datasafe-dbaudit-to-oci-logging/README.md)
4545
- [Importing your own key into OCI Vault](shared-assets/kms-import-keys/README.md)
46+
- [OCI IAM SDK Example](shared-assets/iam-py-sdk/README.md)
47+
=======
48+
4649

4750
4851
# Useful Links
@@ -68,3 +71,4 @@ Copyright (c) 2024 Oracle and/or its affiliates.
6871
Licensed under the Universal Permissive License (UPL), Version 1.0.
6972

7073
See [LICENSE](https://github.com/oracle-devrel/technology-engineering/blob/main/LICENSE) for more details.
74+
Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
# Pyhton Example of Python app with OCI IAM Domainas Identity Provider
2+
3+
Author: Inge Os
4+
5+
The aim is to show how you may configure SSO between an Pyhton app an instance of Oracle OCI IAM Domain,
6+
using the OCI IAM SDK for Python.
7+
8+
The example builds on an earlier Oracle By Example post, and the download of the Python SDK from an OCI IAM Domain.
9+
10+
The example has been updated to Python 3.0 and PyJWT 2.x.
11+
12+
The instructions is based on [Use Oracle Identity Cloud Service's Software Development Kit (SDK) for Authentication in Python Web Applications](https://www.oracle.com/webfolder/technetwork/tutorials/obe/cloud/idcs/idcs_python_sdk_obe/idcs-python-sdk.html) but note the changes below.
13+
14+
Also consult the README.md in the [git repo](https://github.com/oracle-samples/idm-samples/tree/master/idcs-sdk-sample-apps/python/sampleapp) for details on the sampleapp.
15+
16+
## Requirements
17+
This version of the oracle By Example is updated to and requires:
18+
- Python 3.x
19+
- sqlite3 > sqlite3 3.9
20+
- pip upgraded to latest version
21+
- rust
22+
- python setuptool_rust ```pip install setuptool_rust```
23+
24+
## Envrionment variables
25+
26+
In the text below the following environment variables are used:
27+
- ```$SDK_HOME=/home/idssdk/iam-sdk```
28+
- ```$APP_HOME=/home/idcsapp```
29+
- ```$PYTHON_VENV_HOME=/home/idcssdk/py38```
30+
31+
## Prepare the environment, and download the SDK and the SDK Application
32+
33+
- Install Python 3.x and create a virtual environment
34+
- Enable your virtual environment
35+
- Download or clone the git repo https://github.com/oracle-samples/idm-samples/
36+
- Copy all files under https://github.com/oracle-samples/idm-samples/tree/master/idcs-sdk-sample-apps/python or your local clone to a local app directory
37+
38+
The structure should look like:
39+
```
40+
(py36) [idcssdk@iosjumpv3 iam-sample-app]$ find -O1 .
41+
.
42+
./config.json
43+
./db.sqlite3
44+
./LICENSE.md
45+
./manage.py
46+
./python
47+
./python/__init__.py
48+
./python/settings.py
49+
./python/urls.py
50+
./python/wsgi.py
51+
./README.md
52+
./sampleapp
53+
./sampleapp/__init__.py
54+
./sampleapp/admin.py
55+
./sampleapp/apps.py
56+
./sampleapp/migrations
57+
./sampleapp/migrations/__init__.py
58+
./sampleapp/migrations/__pycache__
59+
./sampleapp/migrations/__pycache__/__init__.cpython-36.pyc
60+
./sampleapp/models.py
61+
./sampleapp/static
62+
./sampleapp/static/css
63+
./sampleapp/static/css/sample-app.css
64+
./sampleapp/static/font
65+
./sampleapp/static/font/UniversLTStd-Ex.ttf
66+
./sampleapp/static/font/good times rg.ttf
67+
./sampleapp/static/images
68+
./sampleapp/static/images/oracle.gif
69+
./sampleapp/static/images/oracle.jpg
70+
./sampleapp/templates
71+
./sampleapp/templates/sampleapp
72+
./sampleapp/templates/sampleapp/about.html
73+
./sampleapp/templates/sampleapp/home.html
74+
./sampleapp/templates/sampleapp/includes
75+
./sampleapp/templates/sampleapp/includes/leftmenu.html
76+
./sampleapp/templates/sampleapp/includes/publicheader.html
77+
./sampleapp/templates/sampleapp/index.html
78+
./sampleapp/templates/sampleapp/login.html
79+
./sampleapp/templates/sampleapp/myProfile.html
80+
./sampleapp/tests.py
81+
./sampleapp/urls.py
82+
./sampleapp/views.py
83+
./solution
84+
./solution/.DS_Store
85+
./solution/python_sdk_sample_app.zip
86+
./THIRD_PARTY_LICENSES.txt
87+
```
88+
89+
## Download SDK and Python application
90+
91+
Download the SDK from the OCI IAM Domain
92+
93+
Navigation to the download pages in your OCI IAM Domain
94+
95+
![files/iam1.gif](files/iam1.gif)
96+
97+
![files/iam2.gif](files/iam2.gif)
98+
99+
![files/iam3.gif](files/iam3.gif)
100+
101+
Unzip the downloaded SDK, and save it, in this example to ```$SDK_HOME``` folder:
102+
103+
```cd $SDK_HOME unzip /usr/tmp/python-23.2.92-2301160723.zip
104+
Archive: /usr/tmp/python-23.2.92-2301160723.zip
105+
inflating: requirements.txt
106+
creating: src/
107+
inflating: src/IdcsClient.py
108+
inflating: src/Constants.py
109+
inflating: .devops_metadata.json
110+
inflating: README.txt
111+
inflating: FileInfo.json
112+
```
113+
114+
## Modified files for Python 3.x and PyJWT 2.x compatibility
115+
116+
First the requirements.txt file requests specific versions. For Python 3.x the package specific should be removed.
117+
A ammened version of [requirements.txt](files/requirements.txt) is added to the repo.
118+
The requirement.txt should look like:
119+
120+
```
121+
requests
122+
six
123+
simplejson
124+
PyJWT
125+
lru-ttl
126+
cryptography
127+
```
128+
129+
The [IdcsClient.py](files/IdcsClient.py) uses depreciated features of PyJWT. Use the modified version of [IdcsClient.py](files/IdcsClient.py)
130+
views.py uses Python 2.x print syntax without ```()```` . Use the ammended version of [views.py](files/views.py)
131+
132+
Download or clone this repo, or download the individual files.
133+
Substitute the files in the $SDK_HOME directory above with these files.
134+
135+
## Summary of code changes
136+
137+
- In requrements.txt remove reference to specific versions, use [requirements.txt](files/requirements.txt)
138+
- In [IdcsClient.py](files/IdcsClient.py) change any references to jwt.decode
139+
```
140+
# Change from
141+
jwt.decode(token, verify=False)
142+
# to
143+
jwt.decode(token, options={"verify_signature": False},algorithms=['RS256'])```
144+
```
145+
These changes is done in [IdcsClient.py](files/IdcsClient.py)
146+
- In view.py, a part for the sample app, Python V2 print syntax is used, without ```()```,
147+
change print statements to Python V3 syntax.
148+
Use the updated version [views.py](files/views.py)
149+
150+
151+
152+
## Create a confidential application in your OCI IAM Domain
153+
154+
The screens are changed from IDCS look&feel to OCI IAM look&feel, but the functionality is exactly the same.
155+
The only difference is that, despite you may check the box, OCI IAM Domains will require HTTPS.
156+
157+
The simple Python app is configured with HTTP, it is recommended to use the OCI Load Balancer as termination point for TLS.
158+
159+
Navigate to your OCI IAM Domain, and select integrated applications
160+
161+
![files/app1.jpg](files/app1.jpg)
162+
163+
Select create application and select create confidential application
164+
165+
![files/app2.jpg](files/app2.jpg)
166+
167+
Fill inn name, leave the others
168+
169+
![files/app3.jpg](files/app3.jpg)
170+
171+
Select configure oauth
172+
173+
![files/app4.jpg](files/app4.jpg)
174+
175+
Tick off Authorization Code and Client Credentials, ticking off HTTP only is not recommended, it will still require HTTPS
176+
177+
![files/app5.jpg](files/app5.jpg)
178+
179+
Enter redirect ULR (user server/callback) and Post-logout redirect
180+
181+
![files/app6.jpg](files/app6.jpg)
182+
183+
copy the client-id and client-secret. They will be required in the client app configuration
184+
185+
![files/app7.jpg](files/app7.jpg)
186+
187+
Activate the application
188+
189+
![files/app8.jpg](files/app8.jpg)
190+
191+
## Prepare the python environment
192+
193+
Activate the Python 3.x virtual environment previously created, and then add all the required Python packages.
194+
Navigate to the directory with the unzipped version of the Python SDK, and edit the file requirements.txt and remove all hard version dependencies.
195+
(modified version of [requirements.txt](file/requirements) )
196+
197+
(These are Python 2.x specific) as follows:
198+
Change from:
199+
```
200+
requests==2.21.0
201+
six==1.12.0
202+
simplejson==3.16.0
203+
PyJWT==2.4.0
204+
lru-ttl==0.0.7
205+
cryptography==3.3.2
206+
```
207+
to:
208+
```
209+
requests
210+
six
211+
simplejson
212+
PyJWT
213+
lru-ttl
214+
cryptography
215+
```
216+
Install the requirements with ```pip install -r requirements.txt```
217+
Please note ```setuptools_rust``` needs to be installed prior to installing the requirements
218+
219+
## Prepare Application
220+
221+
Navigate to the directory where the Python app is unpacked
222+
Clone this repo or download the files as follows:
223+
- ![Constants.py](files/Constants.py) to two locations, ```$APP_HOME/.``` and ```$APP_HOME/sampleapp/.```
224+
- ![IdcsClient.py](files/IdcsClient.py) to ```$APP_HOME/sampleapp/.```
225+
- ![views.py](files/views.py) to ```$APP_HOME/sampleapp/.```
226+
227+
Finally, if the version of sqlite3 installed is below the required version, you may either upgrade sqlite3 or patch the file:
228+
229+
```$PYTHON_VENV_HOME/lib64/python3.6/site-packages/django/db/backends/sqlite3/base.py``` line 66
230+
231+
```if Database.sqlite_version_info < (3, 9, 0):``` to the sqlite3 version you have installed
232+
233+
## Update the application
234+
235+
Edit $APP_HOME/config.json, and fill inn the clientid, client secret from the confidential application created above, and add in the URL to the OCI IAM Domain.
236+
237+
```
238+
cat config.json
239+
{
240+
"ClientId" : "8caf8caf8caf8caf8caf8caf8caf",
241+
"ClientSecret" : "d11fb-d11fb--d11fb-d11fb-d11fb",
242+
"BaseUrl" : "https://idcs-myociiamservice.identity.oraclecloud.com",
243+
"AudienceServiceUrl" : "https://idcs-myociiamservice.identity.oraclecloud.com",
244+
"scope" : "urn:opc:idm:t.user.me openid",
245+
"TokenIssuer" : "https://identity.oraclecloud.com/",
246+
"redirectURL": "http://localhost:8000/callback",
247+
"logoutSufix":"/oauth2/v1/userlogout",
248+
"LogLevel":"INFO",
249+
"ConsoleLog":"True"
250+
}
251+
```
252+
253+
## Initial run of the application
254+
255+
Initially the application require a migration step:
256+
```
257+
cd $APP_HOME
258+
python manage.py migrate
259+
```
260+
## Startup of the application
261+
262+
```
263+
cd $APP_HOME
264+
python manage.py run server
265+
```
266+
Output from the start command
267+
```
268+
python manage.py runserver 10.10.0.144:8080
269+
Watching for file changes with StatReloader
270+
Performing system checks...
271+
272+
System check identified no issues (0 silenced).
273+
October 07, 2024 - 19:24:54
274+
Django version 3.2.25, using settings 'python.settings'
275+
Starting development server at http://10.10.0.144:8080/
276+
Quit the server with CONTROL-C.
277+
```
278+
279+
View the server output from the test server, demonstrates the callback from the OCI IAM Domain.
280+
281+
![files/serveroutput.jpg](files/serveroutput.jpg)
282+
283+
## Test through the browser
284+
285+
Start the application
286+
287+
![files/browser1.jpg](files/browser1.jpg)
288+
289+
Select login
290+
![files/browser2.jpg](files/browser2.jpg)
291+
292+
Redirected to OCI IAM Domain as IDP
293+
![files/browser3.jpg](files/browser3.jpg)
294+
295+
Logged on, select my profile
296+
![files/pytest5.jpg](files/pytest5.jpg)
297+
298+
299+
# Documentation Links
300+
301+
Oracle OCI IAM Domain, license types:
302+
[OCI IAM Domain Types](https://docs.oracle.com/en-us/iaas/Content/Identity/sku/overview.htm)
303+
304+
Use Oracle Identity Cloud Service's Software Development Kit (SDK) for Authentication in Python Web Applications,
305+
original article:
306+
[idcs_python_sdk_obe](https://www.oracle.com/webfolder/technetwork/tutorials/obe/cloud/idcs/idcs_python_sdk_obe/idcs-python-sdk.html)
307+
308+
Git repo for OCI IAM Domain Python Samples:
309+
[idcs-sdk-sample-apps/python](https://github.com/oracle-samples/idm-samples/tree/master/idcs-sdk-sample-apps/python)
310+
311+
312+
313+
# License
314+
315+
Copyright (c) 2024 Oracle and/or its affiliates.
316+
317+
Licensed under the Universal Permissive License (UPL), Version 1.0.
318+
319+
See LICENSE for more details.

0 commit comments

Comments
 (0)