-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrun.py
More file actions
91 lines (77 loc) · 3.04 KB
/
Copy pathrun.py
File metadata and controls
91 lines (77 loc) · 3.04 KB
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
# Example WordPress.com Connect
import requests
import string
import random
import hashlib
import urllib.parse
from flask import abort, redirect, url_for
from flask import make_response
from flask import request
from flask import Flask
from config import wpcc_consts
app = Flask(__name__)
@app.route("/")
def login():
state = ''.join(random.choice(string.ascii_uppercase + string.digits)
for x in range(30))
params = {
"response_type": "code",
"client_id": wpcc_consts['client_id'],
"state": state,
"redirect_uri": wpcc_consts['redirect_url']
}
wpcc_url = wpcc_consts['authenticate_url'] + \
'?' + urllib.parse.urlencode(params)
resp = make_response(
'<html><body><h2>Connect to WordPress.com</h2><a href="' +
wpcc_url +
'"><img src="//s0.wp.com/i/wpcc-button.png" width="231" /></a></body></html>'
)
resp.set_cookie('wpcc_state', state)
return resp
@app.route("/connected")
def connected():
app.logger.info("Connected page accessed")
code = request.args.get('code')
if not code:
return redirect(url_for('login'))
state = request.args.get('state')
if not state:
app.logger.warning('State parameter missing in callback URL')
return 'Warning! State variable missing after authentication'
wpcc_state = request.cookies.get('wpcc_state')
if not wpcc_state:
app.logger.warning('wpcc_state cookie missing')
return 'Warning! State cookie missing. Authentication attempt may have been compromised.'
if state != wpcc_state:
app.logger.warning(
'State mismatch: received %s, expected %s', state, wpcc_state)
return 'Warning! State mismatch. Authentication attempt may have been compromised.'
# Optionally, clear the state cookie after use
payload = {
"client_id": wpcc_consts['client_id'],
"redirect_uri": wpcc_consts['redirect_url'],
"client_secret": wpcc_consts['client_secret'],
"code": code, # The code from the previous request
"grant_type": 'authorization_code'
}
r = requests.post(wpcc_consts['request_token_url'], data=payload)
if 200 == r.status_code:
token_data = r.json()
access_token = token_data.get('access_token', 'No token found')
resp = make_response(
f'<html><body>'
f'<h2>Connected to WordPress.com!</h2>'
f'<p><strong>Access Token:</strong> '
f'<span style="background-color: yellow">{access_token}</span></p>'
f'<p>This token can be used to request more info about the user to the endpoint: '
f'<a href="https://developer.wordpress.com/docs/api/1.1/get/me" target="_blank">https://developer.wordpress.com/docs/api/1.1/get/me</a></p>'
f'<p>Connection successful!</p>'
f'</body></html>'
)
resp.set_cookie('wpcc_state', '', expires=0)
return resp
return 'Error: ' + r.text
if __name__ == "__main__":
app.debug = True
app.run(host='localhost', port=5001)