|
| 1 | +# Flask Yoti # |
| 2 | + |
| 3 | +Description |
| 4 | + |
| 5 | +## Plugin configuration ## |
| 6 | +### General settings ### |
| 7 | + |
| 8 | +* `flask_yoti` is a [Flask Blueprint](http://flask.pocoo.org/docs/0.11/blueprints/) |
| 9 | +and all you have to do to add it to your Flask app is register it like this: |
| 10 | +```python |
| 11 | +# your_flask_project/app.py |
| 12 | +from flask import Flask |
| 13 | +from flask_yoti import flask_yoti_blueprint |
| 14 | + |
| 15 | +app = Flask(__name__) |
| 16 | +app.register_blueprint(flask_yoti_blueprint, url_prefix='/yoti') |
| 17 | +``` |
| 18 | +*Don't forget to set an `app.secret_key` to be able to use `sessions`* |
| 19 | + |
| 20 | +* And then use the following settings to configure the plugin: |
| 21 | + |
| 22 | + |
| 23 | +```python |
| 24 | +# your_flask_project/app.py |
| 25 | + |
| 26 | +... |
| 27 | + |
| 28 | +app.config.update({ |
| 29 | + 'YOTI_APPLICATION_ID': '...', |
| 30 | + 'YOTI_CLIENT_SDK_ID': '...', |
| 31 | + 'YOTI_KEY_FILE_PATH': '...', |
| 32 | + 'YOTI_VERIFICATION_KEY': '...', |
| 33 | + ... |
| 34 | +}) |
| 35 | +``` |
| 36 | +* **`YOTI_APPLICATION_ID`** - **required**, *can be also set by env variable with the same name*<br> |
| 37 | +Your Yoti application's ID, found under the `INTEGRATIONS` tab of your |
| 38 | +Yoti application's settings page ([Yoti Dashboard](https://www.yoti.com/dashboard/)).<br> |
| 39 | +It is used to configure the [Yoti Login Button](https://www.yoti.com/developers/#login-button-setup).<br> |
| 40 | +Example: `ca84f68b-1b48-458b-96bf-963868edc8b6` |
| 41 | + |
| 42 | +* **`YOTI_CLIENT_SDK_ID`** - **required**, *can be also set by env variable with the same name*<br> |
| 43 | +Your Yoti application's SDK ID, found under the `INTEGRATIONS` tab of your |
| 44 | +Yoti application's settings page ([Yoti Dashboard](https://www.yoti.com/dashboard/)).<br> |
| 45 | +Example: `39aef70a-89d6-4644-a687-b3e891613da6` |
| 46 | + |
| 47 | +* **`YOTI_KEY_FILE_PATH`** - **required**, *can be also set by env variable with the same name*<br> |
| 48 | +The full path to your private key downloaded from your Yoti application's |
| 49 | +settings page under the `KEYS` tab ([Yoti Dashboard](https://www.yoti.com/dashboard/)).<br> |
| 50 | +Example: `/home/user/.ssh/access-security.pem` |
| 51 | + |
| 52 | +* **`YOTI_VERIFICATION_KEY`** - *can be also set by env variable with the same name*<br> |
| 53 | +A key, used to verify your callback URL. Can be found under the |
| 54 | +`INTEGRATIONS` tab of your Yoti application's settings page (Callback URL -> VERIFY).<br> |
| 55 | +Example: `b14886f972d0c717` |
| 56 | + |
| 57 | + |
| 58 | +### Endpoints configuration ### |
| 59 | + |
| 60 | +`flask_yoti` plugin provides some default endpoints: |
| 61 | +- `yoti_auth` (`/yoti/auth`) - is used for receiving token via callback and |
| 62 | +should'nt be changed |
| 63 | +- `yoti_login` (`/yoti/login`) - a view with just a login button. Can (and should) |
| 64 | +be overridden by `'YOTI_LOGIN_VIEW'` setting |
| 65 | +- `yoti_profile` (`/yoti/profile`) - a view with user profile details. It's |
| 66 | +also given just for example and should be overridden by your view, using |
| 67 | +`'YOTI_REDIRECT_TO'` setting |
| 68 | + |
| 69 | +```python |
| 70 | +# your_flask_project/app.py |
| 71 | + |
| 72 | +... |
| 73 | + |
| 74 | +app.config.update({ |
| 75 | + ... |
| 76 | + 'YOTI_LOGIN_VIEW': '...', |
| 77 | + 'YOTI_REDIRECT_TO': '...', |
| 78 | + 'YOTI_LOGIN_BUTTON_LABEL': '...', |
| 79 | +}) |
| 80 | +``` |
| 81 | +* **`YOTI_LOGIN_VIEW`**<br> |
| 82 | +If *not* authenticated user is trying to access a view with |
| 83 | +`@yoti_authenticated` decorator, he/she will be redirected to this view. |
| 84 | +Example: `login`<br> |
| 85 | +In case you have something like this in your Flask app: |
| 86 | +```python |
| 87 | +@app.route('/login') |
| 88 | +def login(): |
| 89 | + render_template('login.html') |
| 90 | +``` |
| 91 | +Default value: `flask_yoti.login` (with `/yoti/login/` URL) |
| 92 | + |
| 93 | +* **`YOTI_REDIRECT_TO`**<br> |
| 94 | +View name to which user is redirected after successful authentication.<br> |
| 95 | +Example: `profile`<br> |
| 96 | +In case you have something like this in your Flask app:: |
| 97 | +```python |
| 98 | +@app.route('/profile') |
| 99 | +@yoti_authenticated |
| 100 | +def login(): |
| 101 | + user_profile = session.get('yoti_user_profile') |
| 102 | + render_template('profile.html', **user_profile) |
| 103 | +``` |
| 104 | +Default value: `flask_yoti.profile` (with `/yoti/profile/` URL |
| 105 | + |
| 106 | +<br> |
| 107 | + |
| 108 | + |
| 109 | +### Yoti application configuration ### |
| 110 | + |
| 111 | +Your Yoti application's callback URL should point to `your_site.com/yoti/auth`.<br> |
| 112 | +If you want to add a verification tag into any page (other than `/yoti/auth/`), |
| 113 | +you can use a `{{ yoti_site_verification }}` tag inside 'head' tag of that page. |
| 114 | + |
| 115 | +## Using plugin ## |
| 116 | + |
| 117 | +1. First you need to add a login button to some of your view's templates. |
| 118 | +- You can do it by using one of the predefined login buttons: |
| 119 | +``` |
| 120 | +{{ yoti_login_button_sm }} |
| 121 | +{{ yoti_login_button_md }} |
| 122 | +{{ yoti_login_button_lg }} |
| 123 | +``` |
| 124 | +- or with `{{ yoti_login_button(size='small', text='Log In with Yoti')`<br> |
| 125 | +Available button sizes: `small`, `medium`, `large` |
| 126 | + |
| 127 | +By clicking this button, user will be redirected to the Yoti Authentication page. |
| 128 | + |
| 129 | +*Remember to add an appropriate script to your page with login |
| 130 | +button in order for it to work. See: [Yoti Developers Documentation](https://www.yoti.com/developers/#login-button-setup)* |
| 131 | + |
| 132 | +2. After successful authentication, user will be redirected to a view, |
| 133 | +provided by the `YOTI_REDIRECT_TO` setting. |
| 134 | +3. In order to have an access to an authenticated user's information inside a view, |
| 135 | +you should use a `@yoti_authenticated` decorator. |
| 136 | +Example: |
| 137 | +```python |
| 138 | +from flask_yoti import yoti_authenticated |
| 139 | + |
| 140 | +@yoti_authenticated |
| 141 | +def profile_view(request): |
| 142 | + user_id = request.yoti_user_id |
| 143 | + user_profile = request.yoti_user_profile |
| 144 | + return render(request, 'profile.html', user_profile) |
| 145 | +``` |
| 146 | + |
| 147 | +4. All *not authenticated* users trying to access endpoint with this decorator, |
| 148 | +will be redirected to an endpoint, provided by the `YOTI_LOGIN_VIEW` setting. |
| 149 | + |
| 150 | +## Tests ## |
| 151 | + |
| 152 | +To run unit tests just type `py.test` inside `flask_yoti` dir. |
0 commit comments