Skip to content

Commit 3d6ceca

Browse files
committed
added jira oauth2 example
1 parent ef4757f commit 3d6ceca

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

examples/jira/jira_oauth2.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Example server with Flask demonstrating use of Jira OAuth 2.0.
2+
# Server needs to be deployed. Example code is requesting access token from
3+
# Jira. User has to grant access rights. After authorization the
4+
# token and Using access token, Jira cloud ID is identified and
5+
# the available projects are returned.
6+
7+
from requests_oauthlib import OAuth2Session
8+
from atlassian.jira import Jira
9+
from flask import Flask, request, redirect, session
10+
import requests
11+
12+
app = Flask(__name__)
13+
app.secret_key = ""
14+
15+
# JIRA OAuth URLs
16+
authorization_base_url = "https://auth.atlassian.com/authorize"
17+
token_url = "https://auth.atlassian.com/oauth/token"
18+
19+
20+
# Create OAuth 2.0 Integration in Atlassian developer console
21+
# https://developer.atlassian.com/console/myapps/
22+
# Click Authorization → “Configure” under OAuth 2.0 and
23+
# Enter callback url {server}/callback and save
24+
# Click “Permissions” and Add “Jira platform REST API” and other required permissions.
25+
# Click “Configure” under Jira platform REST API and Add permissions like
26+
# “View user profiles“, “View Jira issue data“ and “Create and manage issues”
27+
# Goto setting and copy client id and secret.
28+
29+
client_id = ""
30+
client_secret = ""
31+
redirect_uri = "" # {server_url}/callback
32+
33+
34+
# 2. Redirect to Jira for authorization
35+
# The server request to {server_url}/login is redirected to Jira.
36+
# The user is asked to grant access permissions.
37+
@app.route("/login")
38+
def login():
39+
scope = ["read:me", "read:jira-user", "read:jira-work"]
40+
audience = "api.atlassian.com"
41+
42+
jira_oauth = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri)
43+
authorization_url, state = jira_oauth.authorization_url(
44+
authorization_base_url,
45+
audience=audience,
46+
)
47+
session["oauth_state"] = state
48+
return redirect(authorization_url)
49+
50+
51+
# 3. Jira redirects user to callback url with authorization code
52+
# This should be set to {server_url}/callback.
53+
# Access token is fetched using authorization code
54+
@app.route("/callback")
55+
def callback():
56+
jira_oauth = OAuth2Session(client_id, state=session["oauth_state"], redirect_uri=redirect_uri)
57+
request_url = request.url.replace("http://", "https://")
58+
token_json = jira_oauth.fetch_token(token_url, client_secret=client_secret, authorization_response=request_url)
59+
return "Token: {}<p />Projects: {}".format(token_json, ", ".join(get_projects(token_json)))
60+
61+
62+
# 4. Access Token used for Jira Python API
63+
# Using access token, accessible resources are fetched and
64+
# First resource id is taken as jira cloud id,
65+
# Jira Client library is called with jira cloud id and token information.
66+
def get_projects(token_json):
67+
req = requests.get(
68+
"https://api.atlassian.com/oauth/token/accessible-resources",
69+
headers={
70+
"Authorization": f"Bearer {token_json['access_token']}",
71+
"Accept": "application/json",
72+
},
73+
)
74+
req.raise_for_status()
75+
resources = req.json()
76+
cloud_id = resources[0]["id"]
77+
78+
oauth2_dict = {
79+
"client_id": client_id,
80+
"token": {
81+
"access_token": token_json["access_token"],
82+
"token_type": "Bearer",
83+
},
84+
}
85+
jira = Jira(url=f"https://api.atlassian.com/ex/jira/{cloud_id}", oauth2=oauth2_dict)
86+
return [project["name"] for project in jira.projects()]
87+
88+
89+
if __name__ == "__main__":
90+
app.run(debug=True, port=8000, host="0.0.0.0")

0 commit comments

Comments
 (0)