-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_implicit.py
executable file
·62 lines (50 loc) · 1.68 KB
/
test_implicit.py
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
#!/usr/bin/env python
import json
import codecs
from subprocess import Popen, PIPE
from urllib.parse import urlparse, urlencode, parse_qs
from urllib.request import urlopen, Request
serveraddr = 'localhost:8011'
authorization_endpoint = 'http://'+serveraddr+'/oauth2/auth'
token_endpoint = 'http://'+serveraddr+'/oauth2/token'
userinfo_endpoint = 'http://'+serveraddr+'/api/me'
client_id = '4'
client_secret = '-Ljt2Nt-XqOZlifx4_v0mg'
redirect_uri = 'http://localhost:8180'
utf8reader = codecs.getreader('utf8')
url = authorization_endpoint + '?' + urlencode({
'response_type': 'id_token token', #可以只为 'token'
'redirect_uri': redirect_uri,
'client_id': client_id,
'scope': 'openid',
'state': '0.847524793818593',
})
print ("url is : ",url)
username = "cy"
password = "123456"
p = Popen(['curl', '-v', url, '-d', 'login='+username+'&password='+password],
stdout=PIPE, stderr=PIPE)
for line in p.stderr:
line = line.decode('utf8')
print(line.rstrip())
if line.startswith('< Location: '):
code_callback_url = line.strip()[len('< Location: '):]
break
else:
print(p.stdout.read())
print(code_callback_url)
print(parse_qs(urlparse(code_callback_url).fragment))
accessinfo = parse_qs(urlparse(code_callback_url).fragment)
access_token = accessinfo['access_token'][0]
print("Access Token: ", access_token)
print("> GET {}".format(userinfo_endpoint))
req = Request(userinfo_endpoint, headers={
'Authorization': 'Bearer {}'.format(access_token),
})
print(req)
f = utf8reader(urlopen(req))
userinfo = json.load(f)
print(userinfo)
print("Username:", userinfo['name'])
print("Email:", userinfo['email'])
print("Groups:", ", ".join(userinfo['groups']))