Skip to content

Commit 249d8c2

Browse files
committed
Add rule pattern search example to basic auth0 flask example
1 parent 3befe72 commit 249d8c2

File tree

6 files changed

+161
-23
lines changed

6 files changed

+161
-23
lines changed

01-Login/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ AUTH0_DOMAIN={DOMAIN}
33
AUTH0_CLIENT_SECRET={CLIENT_SECRET}
44
AUTH0_CALLBACK_URL=http://localhost:3000/callback
55
AUTH0_AUDIENCE=
6+
AUTH0_MAN_JWT={MAN_AUTH_JWT}

01-Login/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
REDIRECT_URI_KEY = 'redirect_uri'
1919
SECRET_KEY = 'ThisIsTheSecretKey'
2020
JWT_PAYLOAD = 'jwt_payload'
21+
MANAGEMENT_JWT = 'AUTH0_MAN_JWT'
2122

01-Login/rule_functions.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import urllib3
2+
import json
3+
4+
5+
def getRulesMatching(variable, pattern, domain, manage_jwt) :
6+
# Setup request with content type and auth headers
7+
http = urllib3.PoolManager()
8+
req = http.request("GET", "https://{0}/api/v2/rules".format(domain),
9+
headers={
10+
"content-type": "application/json",
11+
"Authorization" : "Bearer {0}".format(manage_jwt)
12+
})
13+
14+
print("Got server response : {0}".format(req.status))
15+
16+
if req.status != 200:
17+
print("Error in server response code")
18+
return None
19+
20+
json_data = json.loads(req.data.decode('utf-8'))
21+
22+
print(len(json_data))
23+
24+
rules_matching_str = []
25+
26+
for json_entity in json_data:
27+
# Print rule name
28+
print("Rule : {0}".format(json_entity['name']))
29+
30+
# Check if this rule has the "clientName" comparison in its script
31+
if variable in json_entity['script']:
32+
# Split the script on newlines
33+
lines = json_entity['script'].split("\n")
34+
35+
# Loop and check if this line contains the comment
36+
for line in lines:
37+
if variable in line :
38+
# If the comment is found split the line at '===' & '\''
39+
40+
delim = None
41+
42+
if pattern in line:
43+
delim = pattern
44+
45+
if delim is not None:
46+
apps = line.split(delim)[1]
47+
apps = apps.split('\'')[1]
48+
49+
rule = {}
50+
rule['title'] = apps
51+
rule['script'] = json_entity['script']
52+
print("Adding rule {0} : pattern {1}".format(apps, pattern))
53+
rules_matching_str.append(rule)
54+
55+
else :
56+
print("No app definitions found in this rule")
57+
58+
if len(rules_matching_str) > 0:
59+
return rules_matching_str
60+
else:
61+
return None

01-Login/server.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
from flask_oauthlib.client import OAuth
1616
from six.moves.urllib.parse import urlencode
1717
import requests
18-
18+
import rule_functions
1919
import constants
20+
from urllib.parse import unquote
2021

2122
ENV_FILE = find_dotenv()
2223
if ENV_FILE:
@@ -27,6 +28,8 @@
2728
AUTH0_CLIENT_SECRET = env.get(constants.AUTH0_CLIENT_SECRET)
2829
AUTH0_DOMAIN = env.get(constants.AUTH0_DOMAIN)
2930
AUTH0_AUDIENCE = env.get(constants.AUTH0_AUDIENCE)
31+
AUTH0_MANAGEMENT_JWT = env.get(constants.MANAGEMENT_JWT)
32+
3033
if AUTH0_AUDIENCE is '':
3134
AUTH0_AUDIENCE = 'https://' + AUTH0_DOMAIN + '/userinfo'
3235

@@ -49,10 +52,13 @@ def handle_auth_error(ex):
4952
return response
5053

5154

52-
@APP.errorhandler(Exception)
53-
def handle_auth_error(ex):
54-
response = jsonify(message=ex.message)
55-
return response
55+
#@APP.errorhandler(Exception)
56+
#def handle_auth_error(ex):
57+
# try :
58+
# response = jsonify(message=ex.message)
59+
# return response
60+
# except :
61+
# return "Unable to process error"
5662

5763
oauth = OAuth(APP)
5864

@@ -122,9 +128,38 @@ def logout():
122128
return redirect(auth0.base_url + '/v2/logout?' + urlencode(params))
123129

124130

131+
@APP.route('/get_rules')
132+
@requires_auth
133+
def get_rules():
134+
variable = request.args.get('variable')
135+
pattern = request.args.get('pattern')
136+
137+
pattern = unquote(pattern, 'utf-8')
138+
variable = unquote(variable, 'utf-8')
139+
140+
print(variable)
141+
142+
if '"' in pattern:
143+
pattern = pattern.replace('"', '')
144+
145+
if variable is None or pattern is None:
146+
return redirect(url_for("home"))
147+
148+
try :
149+
rules = rule_functions.getRulesMatching(variable, pattern, AUTH0_DOMAIN, AUTH0_MANAGEMENT_JWT)
150+
except Exception:
151+
rules = []
152+
153+
if len(rules) < 1:
154+
return redirect(url_for("dashboard"))
155+
156+
return render_template("list_rules.html", rules=rules)
157+
158+
125159
@APP.route('/dashboard')
126160
@requires_auth
127161
def dashboard():
162+
128163
return render_template('dashboard.html',
129164
userinfo=session[constants.PROFILE_KEY],
130165
userinfo_pretty=json.dumps(session[constants.JWT_PAYLOAD], indent=4))

01-Login/templates/dashboard.html

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
<html>
2-
<head>
3-
<meta name="viewport" content="width=device-width, initial-scale=1">
2+
<head>
3+
<meta name="viewport" content="width=device-width, initial-scale=1">
4+
5+
<!-- font awesome from BootstrapCDN -->
6+
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
7+
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
8+
<link href="/public/app.css" rel="stylesheet">
9+
</head>
10+
<body class="home">
11+
<div class="container">
12+
<div class="login-page clearfix">
13+
<div class="logged-in-box auth0-box logged-in">
14+
<h1 id="logo"><img src="//cdn.auth0.com/samples/auth0_logo_final_blue_RGB.png"/></h1>
15+
<h2>Welcome {{userinfo['name']}}</h2>
16+
</div>
17+
<div>
18+
<form action="{{url_for('get_rules')}}">
19+
<div class="form-group">
20+
<label for="variableName">Variable Name</label>
21+
<input type="text" name="variable" class="form-control" id="variableName" aria-describedby="variableHelp">
22+
<small id="variableHelp" class="form-text text-muted">Variable to lookup</small>
23+
</div>
24+
<div class="form-group">
25+
<label for="pattern">Pattern</label>
26+
<input type="text" name="pattern" class="form-control" id="pattern" aria-describedby="patternHelp">
27+
<small id="patternHelp" class="form-text text-muted">Pattern to lookup</small>
428

5-
<!-- font awesome from BootstrapCDN -->
6-
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
7-
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
8-
<link href="/public/app.css" rel="stylesheet">
9-
</head>
10-
<body class="home">
11-
<div class="container">
12-
<div class="login-page clearfix">
13-
<div class="logged-in-box auth0-box logged-in">
14-
<h1 id="logo"><img src="//cdn.auth0.com/samples/auth0_logo_final_blue_RGB.png" /></h1>
15-
<img class="avatar" src="{{userinfo['picture']}}"/>
16-
<h2>Welcome {{userinfo['name']}}</h2>
17-
<pre>{{userinfo_pretty}}</pre>
18-
<a class="btn btn-primary btn-lg btn-logout btn-block" href="/logout">Logout</a>
1929
</div>
20-
</div>
30+
<button type="submit" class="btn btn-primary btn-logout btn-block">Lookup Rule</button>
31+
</form>
2132
</div>
22-
</body>
33+
</div>
34+
<div>
35+
<br> <br>
36+
<a class="btn btn-primary btn-logout btn-block" href="/logout">Logout</a>
37+
</div>
38+
</div>
39+
</body>
2340
</html>

01-Login/templates/list_rules.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<meta name="viewport" content="width=device-width, initial-scale=1">
4+
5+
<!-- font awesome from BootstrapCDN -->
6+
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
7+
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
8+
<link href="/public/app.css" rel="stylesheet">
9+
</head>
10+
<body class="home">
11+
<div class="container">
12+
<div class="login-page clearfix">
13+
{% for rule in rules %}
14+
<div class="auth0-box">
15+
<h2>{{ rule.title }}</h2>
16+
<pre>{{ rule.script }}</pre>
17+
</div>
18+
{% endfor %}
19+
<a class="btn btn-primary btn-lg btn-logout btn-block" href="/logout">Logout</a>
20+
</div>
21+
</div>
22+
</body>
23+
</html>

0 commit comments

Comments
 (0)