FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Procore-Sample-Python/oauth_main.py at master · procore/Procore-Sample-Python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Aug 10, 2026. It is now read-only.
procore
/
Procore-Sample-Python
Public archive
Notifications
You must be signed in to change notification settings
Fork
8
Star
13
Code
Issues
3
Pull requests
5
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
Procore-Sample-Python
/
oauth_main.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
210 lines (180 loc) · 6.86 KB
Breadcrumbs
Procore-Sample-Python
/
oauth_main.py
Copy path
File metadata and controls
210 lines (180 loc) · 6.86 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# --------------------------- IMPORTS --------------------------- #
from
datetime
import
datetime
import
os
import
urllib
from
flask
import
Flask
,
abort
,
request
,
session
,
redirect
,
render_template
import
requests
import
requests
.
auth
from
dotenv
import
load_dotenv
# ------------------------- END IMPORTS ------------------------- #
# adding session may have fixed the problem with session variable runtime error.
session
=
{}
session
[
'bool'
]
=
False
# loads the environment variables
load_dotenv
()
CLIENT_ID
=
os
.
getenv
(
"CLIENT_ID"
)
CLIENT_SECRET
=
os
.
getenv
(
"CLIENT_SECRET"
)
REDIRECT_URI
=
os
.
getenv
(
"REDIRECT_URI"
)
OAUTH_URL
=
os
.
getenv
(
"OAUTH_URL"
)
BASE_URL
=
os
.
getenv
(
"BASE_URL"
)
# end loading env variables
def
gen_secret_key
():
"""
DESCRIPTION:
Generates SECRET_KEY, used for existence session variables
INPUT:
N/A
OUTPUT:
return = randomly generated SECRET_KEY
"""
return
os
.
urandom
(
16
)
def
gen_token_disp
(
token
):
"""
DESCRIPTION:
Grabs the first and last 7 characters of the token and appends them to '...'
ex: D5tWEds...565EER_
INPUT:
token = access_token/refresh_token
OUTPUT:
return = concatinated string of auth
"""
return
token
[:
7
]
+
'...'
+
token
[
-
7
:]
def
update_date
(
created_at
):
'''
DESCRIPTION:
Turns unix time stamp into human readable time for the expire_date.Takes in unix created_at
time value and adds 7200 (2 hours) to the created_at value before converting time.
INPUTS:
created_at = the unix date and time of the authorization token creation.
OUTPUT:
return = returns the created_at unix date/time in a human-readable format
'''
return
datetime
.
utcfromtimestamp
(
created_at
).
strftime
(
'%Y-%m-%d %H:%M:%S'
)
def
update_expire
(
created_at
):
'''
DESCRIPTION:
Turns unix time stamp into human readable time for the expire_date.Takes in unix created_at
time value and adds 7200 (2 hours) to the created_at value before converting time.
INPUTS:
created_at = the unix date and time of the authorization token creation.
OUTPUT:
return = returns the expires_at unix date/time in a human-readable format.
'''
return
datetime
.
utcfromtimestamp
(
created_at
+
7200
).
strftime
(
'%Y-%m-%d %H:%M:%S'
)
def
get_me
(
access_token
):
'''
DESCRIPTION:
Calls /rest/v1.0/me endpoint and returns user login/id.
INPUTS:
access_token = access_token used as credentials to communicate with the API
OUTPUTS:
me_json['login'] = user's login name
me_json['id'] = user's login ID
'''
headers
=
{
"Authorization"
:
"Bearer "
+
access_token
}
response
=
requests
.
get
(
BASE_URL
+
"/rest/v1.0/me"
,
headers
=
headers
)
me_json
=
response
.
json
()
return
me_json
[
'login'
],
me_json
[
'id'
]
def
make_authorization_url
():
'''
DESCRIPTION:
Creates the authorization URL to obtain the authorization code from Procore.
INPUTS:
N/A
OUTPUTS:
url: the url used to obtain the authorization code from the application.
'''
# Generate a random string for the state parameter
# Save it for use later to prevent xsrf attacks
params
=
{
"client_id"
:
CLIENT_ID
,
"response_type"
:
"code"
,
"redirect_uri"
:
REDIRECT_URI
}
url
=
OAUTH_URL
+
"/oauth/authorize?"
+
urllib
.
parse
.
urlencode
(
params
)
return
url
def
get_token
(
code
):
'''
DESCRIPTION:
Gets the access token by utilizating the authorization code that was
previously obtained from the authorization_url call.
INPUTS:
code = authorization code
OUTPUTS:
response_json["access_token"] = user's current access token
response_json["refresh_token"] = user's current refresh token
response_json['created_at'] = the date and time the user's access
token was generated
'''
client_auth
=
requests
.
auth
.
HTTPBasicAuth
(
CLIENT_ID
,
CLIENT_SECRET
)
post_data
=
{
"grant_type"
:
"authorization_code"
,
"code"
:
code
,
"redirect_uri"
:
REDIRECT_URI
}
response
=
requests
.
post
(
BASE_URL
+
"/oauth/token"
,
auth
=
client_auth
,
data
=
post_data
)
response_json
=
response
.
json
()
return
response_json
[
"access_token"
],
response_json
[
"refresh_token"
],
response_json
[
'created_at'
]
# -------------------------app REDIRECTS --------------------------- #
app
=
Flask
(
__name__
)
# Need to use session variables which help us carry values of different webpages
app
.
secret_key
=
gen_secret_key
()
@
app
.
route
(
'/'
,
methods
=
[
"GET"
,
"POST"
])
def
app_homepage
():
return
render_template
(
'login.html'
)
@
app
.
route
(
'/get_auth'
,
methods
=
[
"POST"
])
def
app_auth
():
return
redirect
(
make_authorization_url
())
@
app
.
route
(
'/users/home'
,
methods
=
[
'POST'
,
'GET'
])
def
app_callback
():
if
request
.
method
==
"GET"
:
if
session
.
get
(
'bool'
)
is
False
:
code
=
request
.
args
.
get
(
'code'
)
access_token
,
refresh_token
,
created_at
=
get_token
(
code
)
session
[
'access_token'
]
=
access_token
session
[
'refresh_token'
]
=
refresh_token
session
[
'created_at'
]
=
update_date
(
created_at
)
session
[
'expires_at'
]
=
update_expire
(
created_at
)
session
[
'bool'
]
=
True
html
=
render_template
(
'home.html'
)
return
html
.
format
(
gen_token_disp
(
session
.
get
(
'access_token'
)),
session
.
get
(
'created_at'
),
session
[
'expires_at'
],
gen_token_disp
(
session
[
'refresh_token'
])
)
@
app
.
route
(
'/users/me'
,
methods
=
[
"GET"
])
def
app_page_me
():
[
login
,
my_id
]
=
get_me
(
session
.
get
(
'access_token'
))
html
=
render_template
(
'me.html'
)
return
html
%
(
my_id
,
login
)
@
app
.
route
(
'/users/refreshtoken'
,
methods
=
[
"POST"
])
def
app_refresh_token
():
access_token
=
session
.
get
(
'access_token'
)
refresh_token
=
session
.
get
(
'refresh_token'
)
headers
=
{
"Authorization"
:
"Bearer "
+
access_token
}
data
=
{
"client_id"
:
CLIENT_ID
,
"grant_type"
:
"refresh_token"
,
"redirect_uri"
:
REDIRECT_URI
,
"client_secret"
:
CLIENT_SECRET
,
"refresh_token"
:
refresh_token
}
response
=
requests
.
post
(
BASE_URL
+
'/oauth/token'
,
data
=
data
,
headers
=
headers
)
response_json
=
response
.
json
()
session
[
'access_token'
]
=
response_json
[
'access_token'
]
session
[
'refresh_token'
]
=
response_json
[
'refresh_token'
]
session
[
'created_at'
]
=
update_date
(
response_json
[
'created_at'
])
session
[
'expires_at'
]
=
update_expire
(
response_json
[
'created_at'
])
return
redirect
(
'/users/home'
)
@
app
.
route
(
'/users/revoketoken'
,
methods
=
[
'POST'
])
def
app_revoke_token
():
access_token
=
session
.
get
(
'access_token'
)
data
=
{
"client_id"
:
CLIENT_ID
,
"client_secret"
:
CLIENT_SECRET
,
"token"
:
access_token
}
requests
.
post
(
BASE_URL
+
'/oauth/token'
,
data
=
data
)
return
redirect
(
'/'
)
Back
|
FazBrowse Home
|
New Git URL