-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update ex-01 and friends after run-11 (#39)
* Remove ex-01 checks for zsh as zsh now is default * Removing usage of vscode rest-client from ex-01 * Add python code for first and second leg in ex-01 * Stop using Github Codespace User secret for ex-01 client_secret * Optimize cloc part of ex-04
- Loading branch information
Showing
14 changed files
with
175 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,53 @@ | ||
# Requesting an Access Token using using Rest Client. | ||
# Requesting an Access Token | ||
|
||
Now we are continuing on the second leg of the auth code flow, using the acquired code to request a token. | ||
|
||
## Registering the client secret | ||
|
||
The client need to authenticate itself with the Authorization Server (Microsoft Entra ID). For this it uses a client secret. | ||
|
||
Steps: | ||
|
||
* Explore the `POST` request in 'authCode.http' | ||
* Copy the one-time `Code` from previous exercise (leg 1) to `&code=` of the post request | ||
* Select "Send the request" in VSCode (just above the POST definition) | ||
* Explore the results in the 'Response window' | ||
|
||
* Register a client secret for your application (In 'Certificates and Secrets') | ||
* Expire: 7 days | ||
* Copy the secret *value* (not the secret id) | ||
* Execute the following command to make the client secret available to the environment (add your secret value) | ||
|
||
```shell | ||
export CLIENT_SECRET='THE-VALUE-OF-YOUR-CLIENT-SECRET' | ||
``` | ||
|
||
## --Now You-- | ||
|
||
* Do the steps | ||
|
||
|
||
## Getting the access token | ||
|
||
Steps: | ||
|
||
* We assume your terminal window is in `./ex-01` | ||
* Open and explore `./ex-01/second-leg.py` file in VSCode. | ||
* Update values for the following variables: | ||
* Your tenant id (`tenant_id`) | ||
* Your client id (`client_id`) | ||
* The redirect URI (`redirect_uri`) | ||
* The One time code (`authorization_code`) (use code from first leg) | ||
* (The client_secret is read from the terminal environment) | ||
* Execute `second-leg.py` | ||
|
||
```shell | ||
python ./second-leg.py | ||
```` | ||
## --Now You-- | ||
* Do the steps | ||
* Resend the POST request with the same o-t-code - observe results | ||
## --Discuss security issues and good practices-- | ||
* This part of the communication happens in the "back-channel" | ||
(will be more obvious later on) | ||
* This part of the communication happens in the "back-channel" (will be more obvious later on) | ||
* Public vs. Confidential Client (Trust level) | ||
* The importance of proper SSL, exception of localhost | ||
* The importance of handling `client_secret` as a secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import urllib.parse | ||
|
||
# OAuth2 configuration | ||
tenant_id = "YOUR_TENANT_ID" | ||
client_id = "YOUR_CLIENT_ID" | ||
redirect_uri = "YOUR_REDIRECT_URI" | ||
authorization_endpoint = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize" | ||
|
||
# Requested permissions/scope | ||
scope = "openid profile" | ||
state = "1234" # optional but recommended for security, CSRF | ||
|
||
# Construct the authorization URL | ||
params = { | ||
"response_type": "code", | ||
"client_id": client_id, | ||
"redirect_uri": redirect_uri, | ||
"scope": scope, | ||
"state": state | ||
} | ||
|
||
authorization_url = authorization_endpoint + "?" + urllib.parse.urlencode(params) | ||
print("Authorization URL:\n\n", authorization_url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requests==2.32.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import requests | ||
import os | ||
import json | ||
|
||
# OAuth2 configuration | ||
tenant_id = "YOUR_TENANT_ID" | ||
client_id = "YOUR_CLIENT_ID" | ||
redirect_uri = "YOUR_REDIRECT_URI" | ||
|
||
# Getting client secret from the environment | ||
client_secret = os.getenv("CLIENT_SECRET") | ||
|
||
# Calculate the token endpoint | ||
token_endpoint = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" | ||
|
||
# Authorization code received in the redirect | ||
authorization_code = "AUTHORIZATION_CODE_FROM_FIRST_LEG" | ||
|
||
# Request parameters | ||
data = { | ||
"grant_type": "authorization_code", | ||
"code": authorization_code, | ||
"redirect_uri": redirect_uri, | ||
"client_id": client_id, | ||
"client_secret": client_secret | ||
} | ||
|
||
# Send the POST request to get the access token | ||
response = requests.post(token_endpoint, data=data) | ||
|
||
# Check if the request was successful | ||
if response.status_code == 200: | ||
# Parse the JSON response | ||
tokens = response.json() | ||
print(json.dumps(tokens, indent=4)) | ||
else: | ||
print("Failed to obtain token:\n", json.dumps(json.loads(response.text),indent=4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters