### Install Flask-OpenID with easy_install Source: https://pythonhosted.org/Flask-OpenID/index This command installs the Flask-OpenID extension using the easy_install package manager. Ensure easy_install is available in your environment. ```bash $ easy_install Flask-OpenID ``` -------------------------------- ### Install Flask-OpenID with pip Source: https://pythonhosted.org/Flask-OpenID/index This command installs the Flask-OpenID extension using the pip package manager. It's a common method for installing Python packages. ```bash $ pip install Flask-OpenID ``` -------------------------------- ### Initialize Flask-OpenID with Flask App Source: https://pythonhosted.org/Flask-OpenID/index Demonstrates two ways to initialize the Flask-OpenID extension. The first method binds the OpenID instance directly to a Flask application instance upon creation. The second method involves creating the OpenID instance first and then initializing it with the Flask application later, typically within an application factory pattern. ```python app = Flask(__name__) db = OpenID(app) ``` ```python oid = OpenID() def create_app(): app = Flask(__name__) oid.init_app(app) return app ``` -------------------------------- ### OpenID Class Initialization Source: https://pythonhosted.org/Flask-OpenID/index The `OpenID` class is the main entry point for the Flask-OpenID extension. It can be initialized either by directly passing the Flask application instance or by calling the `init_app` method later. ```APIDOC ## `flask_openid.OpenID` Class ### Description Simple helper class for OpenID auth. Has to be created in advance like a `Flask` object. ### Parameters #### Path Parameters * **app** (object) - Optional - The application to register this openid controller with. * **fs_store_path** (string) - Optional - If given, this is the name of a folder where the OpenID auth process can store temporary information. If neither is provided, a temporary folder is assumed. This is overridden by the `OPENID_FS_STORE_PATH` configuration key. * **store_factory** (function) - Optional - Alternatively, a function that creates a python-openid store object. * **fallback_endpoint** (string) - Optional - Optionally, a string with the name of a URL endpoint the user should be redirected to if the HTTP referrer is unreliable. By default, the user is redirected back to the application’s index in that case. * **extension_responses** (list) - Optional - A list of OpenID Extensions Response class. * **safe_roots** (list) - Optional - A list of trust roots to support returning to. * **url_root_as_trust_root** (boolean) - Optional - Whether to use the url_root as trust_root. ### Initialization Examples **Mode 1: Binding to a specific Flask application** ```python from flask import Flask from flask_openid import OpenID app = Flask(__name__) db = OpenID(app) ``` **Mode 2: Configuring the application later** ```python from flask import Flask from flask_openid import OpenID oid = OpenID() def create_app(): app = Flask(__name__) oid.init_app(app) return app ``` ``` -------------------------------- ### Initialize OpenID object in Flask Source: https://pythonhosted.org/Flask-OpenID/index Initializes the OpenID object, which is the main entry point for Flask-OpenID. It requires the Flask application instance, a path for storing OpenID information, and a list of safe redirect roots for security. ```python from flask.ext.openid import OpenID oid = OpenID(app, '/path/to/store', safe_roots=[]) ``` -------------------------------- ### Create Profile Form Template (Jinja2) Source: https://pythonhosted.org/Flask-OpenID/index This Jinja2 template renders the form for creating a user profile. It extends a base layout, sets the page title, and includes input fields for name and email. The form action is set to submit to the current URL, and hidden fields preserve the 'next' URL and user input values. A link to sign out is also provided. ```html {% extends "layout.html" %} {% block title %}Create Profile{% endblock %} {% block body %}
Hey! This is the first time you signed in on this website. In order to proceed we need a couple of more information from you:
If you don't want to proceed, you can sign out again. {% endblock %} ``` -------------------------------- ### Create User Profile Page in Flask Source: https://pythonhosted.org/Flask-OpenID/index This route handles the creation of a user profile after a successful login. It checks for existing user sessions or missing OpenID, processes POST requests to create a new user in the database, and renders a profile creation form. Input validation for name and email is included. It redirects to the next URL upon successful profile creation. ```python @app.route('/create-profile', methods=['GET', 'POST']) def create_profile(): if g.user is not None or 'openid' not in session: return redirect(url_for('index')) if request.method == 'POST': name = request.form['name'] email = request.form['email'] if not name: flash(u'Error: you have to provide a name') elif '@' not in email: flash(u'Error: you have to enter a valid email address') else: flash(u'Profile successfully created') db_session.add(User(name, email, session['openid'])) db_session.commit() return redirect(oid.get_next_url()) return render_template('create_profile.html', next=oid.get_next_url()) ``` -------------------------------- ### OpenID Class Methods Source: https://pythonhosted.org/Flask-OpenID/index Provides documentation for various methods within the `OpenID` class, including those for handling login, errors, and session management. ```APIDOC ## `OpenID` Class Methods ### `after_login(f)` #### Description This function will be called after login. It must redirect to a different place and remember the user somewhere. The session is not modified by SimpleOpenID. The decorated function is passed a `OpenIDResponse` object. ### `attach_reg_info(auth_request, keys, optional_keys)` #### Description Attaches sreg and ax requests to the auth request. ### `errorhandler(f)` #### Description Called if an error occurs with the message. By default, `'openid_error'` is added to the session so that `fetch_error()` can fetch that error from the session. Alternatively, it makes sense to directly flash the error. #### Example ```python @oid.errorhandler def on_error(message): flash(u'Error: ' + message) ``` ### `fetch_error()` #### Description Fetches the error from the session. This removes it from the session and returns that error. This method is probably useless if `errorhandler()` is used. ### `get_current_url()` #### Description Returns the current URL + next. ### `get_next_url()` #### Description Returns the URL where we want to redirect to. This will always return a valid URL. ### `get_success_url()` #### Description Returns the internal success URL. ### `init_app(app)` #### Description This callback can be used to initialize an application for the use with this openid controller. ### `loginhandler(f)` #### Description Marks a function as login handler. This decorator injects some more OpenID required logic. Always decorate your login function with this decorator. ### `signal_error(msg)` #### Description Signals an error. It does this by storing the message in the session. Use `errorhandler()` to this method. ### `try_login(identity_url, ask_for=None, ask_for_optional=None, extensions=None, immediate=False)` #### Description This tries to login with the given identity URL. This function must be called from the `login_handler`. The `ask_for` and `ask_for_optional` parameters can be a set of values to be asked from the OpenID provider, where keys in `ask_for` are marked as required, and keys in `ask_for_optional` are marked as optional. The following strings can be used in the `ask_for` and `ask_for_optional` parameters: `aim`, `blog`, `country`, `dob` (date of birth), `email`, `fullname`, `gender`, `icq`, `image`, `jabber`, `language`, `msn`, `nickname`, `phone`, `postcode`, `skype`, `timezone`, `website`, `yahoo`. `extensions` can be a list of instances of OpenID extension requests that should be passed on with the request. If you use this, please make sure to pass the Response classes of these extensions when initializing OpenID. `immediate` can be used to indicate this request should be a so-called checkid_immediate request, resulting in the provider not showing any UI. Note that this adds a new possible response: `SetupNeeded`, which is the server saying it doesn’t have enough information yet to authorize or reject the authentication (probably, the user needs to sign in or approve the trust root). #### Query Parameters * **identity_url** (string) - Required - The identity URL to attempt login with. * **ask_for** (set) - Optional - A set of profile fields to request, marked as required. * **ask_for_optional** (set) - Optional - A set of profile fields to request, marked as optional. * **extensions** (list) - Optional - A list of OpenID extension request instances. * **immediate** (boolean) - Optional - If true, perform an immediate checkid request without user interaction. ``` -------------------------------- ### OpenIDResponse Class Source: https://pythonhosted.org/Flask-OpenID/index Details the `OpenIDResponse` class, which is passed to the `after_login` function and contains information returned from the OpenID provider. ```APIDOC ## `flask_openid.OpenIDResponse` Class ### Description Passed to the `after_login` function. Provides all the information sent from the OpenID provider. The profile information has to be requested from the server by passing a list of fields as `ask_for` to the `try_login()` function. ### Attributes * **aim** (string) - AIM messenger address as string. * **blog** (string) - URL of blog as string. * **country** (string) - The country of the user as specified by ISO3166. * **dob** (date) - Date of birth of the user. * **email** (string) - Email address of the user. * **fullname** (string) - Full name of the user. * **gender** (string) - Gender of the user. * **icq** (string) - ICQ messenger address as string. * **image** (string) - URL of the user's image. * **jabber** (string) - Jabber ID of the user. * **language** (string) - Preferred language of the user. * **msn** (string) - MSN messenger address as string. * **nickname** (string) - Nickname of the user. * **phone** (string) - Phone number of the user. * **postcode** (string) - Postcode of the user. * **skype** (string) - Skype ID of the user. * **timezone** (string) - Timezone of the user. * **website** (string) - URL of the user's website. * **yahoo** (string) - Yahoo messenger ID of the user. ``` -------------------------------- ### Common OpenID Providers Source: https://pythonhosted.org/Flask-OpenID/index Lists common OpenID providers and their corresponding login URLs for easy integration. ```APIDOC ## COMMON_PROVIDERS ### Description A dictionary mapping common provider names to their login URLs. ### Usage This can be used to implement "click button to login" functionality. ### Providers - **google**: URL for Google OpenID login. - **yahoo**: URL for Yahoo OpenID login. - **aol**: URL for AOL OpenID login. - **steam**: URL for Steam OpenID login. ``` -------------------------------- ### User Attributes Source: https://pythonhosted.org/Flask-OpenID/index This section outlines the various attributes that can be retrieved for a user after successful OpenID authentication. ```APIDOC ## User Attributes This endpoint details the user attributes available after OpenID authentication. ### Description Provides access to user profile information obtained via OpenID. ### Parameters #### User Attributes (Object Fields) - **date_of_birth** (datetime) - Optional - The date of birth of the user. - **email** (string) - Optional - The email address of the user. - **extensions** (object) - Optional - Hash of the response object from the OpenID Extension. - **fullname** (string) - Optional - The full name of the user. - **gender** (string) - Optional - The gender of the user (`f` for female and `m` for male). - **icq** (string) - Optional - ICQ messenger number. - **identity_url** (string) - Optional - The OpenID the user used for sign in. - **image** (string) - Optional - URL to profile image. - **jabber** (string) - Optional - Jabber address. - **language** (string) - Optional - The user's preferred language as specified by ISO639. - **month_of_birth** (integer) - Optional - The month of birth of the user (1-based). - **msn** (string) - Optional - MSN name. - **nickname** (string) - Optional - Desired nickname of the user. - **phone** (string) - Optional - Phone number of the user. - **postcode** (string) - Optional - Free text conforming to the user's country's postal system. - **skype** (string) - Optional - Skype name. - **timezone** (string) - Optional - Timezone string from the TimeZone database. - **website** (string) - Optional - URL of website. - **yahoo** (string) - Optional - Yahoo Messenger address. - **year_of_birth** (integer) - Optional - The year of birth of the user. ``` -------------------------------- ### Handle OpenID Errors with a Custom Handler Source: https://pythonhosted.org/Flask-OpenID/index Shows how to define a custom error handler for Flask-OpenID using the `@oid.errorhandler` decorator. This allows for custom error message display, such as flashing the error to the user. ```python @oid.errorhandler def on_error(message): flash(u'Error: ' + message) ``` -------------------------------- ### HTML login form for Flask-OpenID Source: https://pythonhosted.org/Flask-OpenID/index A basic HTML template for an OpenID login form. It includes fields for the OpenID identifier, a submit button, and hidden fields for 'next' (redirect URL) and potential error messages, which are populated by Flask-OpenID. ```html {% extends "layout.html" %} {% block title %}Sign in{% endblock %} {% block body %}