This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from shupp/client-credentials-example
Added client_credentials example
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import os | ||
import sys | ||
import urllib | ||
import urlparse | ||
import json | ||
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.error import UserNotAuthenticated | ||
from oauth2.store.memory import ClientStore, TokenStore | ||
from oauth2.tokengenerator import Uuid4 | ||
from oauth2.web import Wsgi | ||
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 = Wsgi(server=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() |