forked from wndhydrnt/python-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_credentials_grant.py
71 lines (54 loc) · 2.09 KB
/
client_credentials_grant.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
63
64
65
66
67
68
69
70
71
import os
import sys
import signal
from multiprocessing.process import Process
from wsgiref.simple_server import make_server, WSGIRequestHandler
sys.path.insert(0, os.path.abspath(os.path.realpath(__file__) + '/../../../'))
from oauth2 import Provider
from oauth2.store.memory import ClientStore, TokenStore
from oauth2.tokengenerator import Uuid4
from oauth2.web.wsgi import Application
from oauth2.grant import ClientCredentialsGrant
class OAuthRequestHandler(WSGIRequestHandler):
"""
Request handler that enables formatting of the log messages on the console.
This handler is used by the python-oauth2 application.
"""
def address_string(self):
return "python-oauth2"
def run_auth_server():
try:
client_store = ClientStore()
client_store.add_client(client_id="abc", client_secret="xyz",
redirect_uris=[])
token_store = TokenStore()
token_gen = Uuid4()
token_gen.expires_in['client_credentials'] = 3600
auth_controller = Provider(
access_token_store=token_store,
auth_code_store=token_store,
client_store=client_store,
token_generator=token_gen)
auth_controller.add_grant(ClientCredentialsGrant())
app = Application(provider=auth_controller)
httpd = make_server('', 8080, app, handler_class=OAuthRequestHandler)
print("Starting implicit_grant oauth2 server on http://localhost:8080/...")
httpd.serve_forever()
except KeyboardInterrupt:
httpd.server_close()
def main():
auth_server = Process(target=run_auth_server)
auth_server.start()
print("To test getting an auth token, execute the following curl command:")
print(
"curl --ipv4 -v -X POST"
" -d 'grant_type=client_credentials&client_id=abc&client_secret=xyz' "
"http://localhost:8080/token"
)
def sigint_handler(signal, frame):
print("Terminating server...")
auth_server.terminate()
auth_server.join()
signal.signal(signal.SIGINT, sigint_handler)
if __name__ == "__main__":
main()