Skip to content

Commit

Permalink
added example
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesholcombe committed Jan 7, 2022
1 parent b8e8a2b commit d242a46
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Overview

Do you want to build an app which pulls user data from Google, Spotify, Slack etc?
Do you want to build a Plotly Dash app which pulls user data from Google, Spotify, Slack etc?

**Dash-auth-external** provides a simple interface to authenticate users through OAuth2 code flow. Allowing developers to serve user tailored content.

Expand All @@ -15,7 +15,18 @@ Do you want to build an app which pulls user data from Google, Spotify, Slack et
pip install dash-auth-external
```
## Simple Usage
TODO
```
auth = DashAuthExternal()
server =
```




Expand Down
14 changes: 11 additions & 3 deletions dash_auth_external/auth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Flask
import flask
from werkzeug.routing import RoutingException, ValidationError
from .routes import make_access_token_route, make_auth_route
from urllib.parse import urljoin
import os
Expand All @@ -21,7 +22,13 @@ def get_token(self):
Returns:
str: Bearer Access token from your OAuth2 Provider
"""
return flask.request.cookies.get(self._token_cookie)

try:
return flask.request.cookies.get(self._token_cookie)
except RuntimeError:
raise ValueError(
"This method must be called in a callback as it makes use of the flask request context."
)

def __init__(
self,
Expand Down Expand Up @@ -60,7 +67,7 @@ def __init__(
Returns:
DashAuthExternal: Main package class
"""
"""
app = Flask(__name__, instance_relative_config=False)

self._token_cookie = _token_cookie
Expand Down Expand Up @@ -88,9 +95,10 @@ def __init__(
external_token_url=external_token_url,
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
redirect_suffix=redirect_suffix,
_home_suffix=home_suffix,
token_request_headers=token_request_headers,
)

return app
self.server = app
7 changes: 4 additions & 3 deletions dash_auth_external/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import hashlib
from requests_oauthlib import OAuth2Session

os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"


def make_code_challenge(length: int = 40):
code_verifier = base64.urlsafe_b64encode(os.urandom(length)).decode("utf-8")
Expand All @@ -27,11 +29,11 @@ def make_auth_route(
with_pkce: bool = True,
client_secret: str = None,
scope: str = None,
auth_request_headers: dict = None,
):
@app.route(auth_suffix)
def get_auth_code():
"""Step 1: User Authorization.
"""
Redirect the user/resource owner to the OAuth provider
using an URL with a few key OAuth parameters.
"""
Expand All @@ -44,7 +46,6 @@ def get_auth_code():
# TODO implement this myself
oauth_session = OAuth2Session(
client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=scope,
)
Expand Down
30 changes: 30 additions & 0 deletions examples/usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from dash_auth_external import DashAuthExternal
from dash import Dash, Input, Output, html, dcc

#using spotify as an example
AUTH_URL = "https://accounts.spotify.com/authorize"
TOKEN_URL = "https://accounts.spotify.com/api/token"
CLIENT_ID = "YOUR_CLIENT_ID"

# creating the instance of our auth class
auth = DashAuthExternal(AUTH_URL, TOKEN_URL, CLIENT_ID)
server = (
auth.server
) # retrieving the flask server which has our redirect rules assigned


app = Dash(__name__, server=server) # instantiating our app using this server

##Below we can define our dash app like normal
app.layout = html.Div([html.Div(id="example-output"), dcc.Input(id="example-input")])


@app.callback(Output("example-output", "children"), Input("example-input", "value"))
def example_callback(value):
token = (
auth.get_token()
) ##The token can only be retrieved in the context of a dash callback
return token

if __name__ == "__main__":
app.run_server()

0 comments on commit d242a46

Please sign in to comment.