Mastering Python for Web Development

Mastering Python for Web Development

Web Development is the building and maintenance of websites. Python does this using web frameworks such as Django and Flask. Flask is a lightweight framework which provides developers flexibility and accessibility as one can build a web application quickly using only a single Python file

In this article, I document how I created a Python Login Authentication System using LoginRadius and Flask. LoginRadius is a cloud-based consumer identity and access management (CIAM) platform that allows seamless user authentication and SSO integration into your application. Its advantages are that:

  • simplicity of use
  • provides unmatched data, user, and account security.
  • can be personalized with ease

To begin one needs to create an account in the LoginRadius website. Upon with one accesses the dashboard to retrieve the API credentials-you need to copy the APP Name, API Key, and API Secret and store them somewhere secure and easily retrievable.

cred.png

A whitelisted domain is a trusted location of a website where a user can communicate and collaborate with that domain. By default, LoginRadius whitelists your local computer (localhost).

whitelist.png

One can personalize their system according to their taste on the Auth Page tab. Afterwards, install the LoginRadius Python SDK. This provides functionalities that allow Python programs to communicate with LoginRadius APIs.

pip install LoginRadius-v2 requests cryptography pbkdf2

Then install Flask framework

pip install flask

Create a server.py file where we code

Create a Replace the values of the API_KEY and API_SECRET variables with your LoginRadius application keys we saved earlier to initialize LoginRadius SDK.

from LoginRadius import LoginRadius as LR

LR.API_KEY = "API Key"
LR.API_SECRET = "API Secret"
loginradius = LR()

Registering users

The APP_NAME is your LoginRadius app name from the dashboard. AUTH_ACTION is the authentication action you’re attempting to perform-can be either register or login. RETURN_URL refers to the URL LoginRadius should redirect your users to after successful authentication. It is usually a route on your application server.

https://{APP_NAME}.hub.loginradius.com/auth.aspx?action={AUTH_ACTION}&return_url={RETURN_URL}

Then we create a register route that redirects users to our LoginRadius registration IDX. We also set our AUTH_ACTION to “register” and our RETURN_URL to our application home page.

@app.route("/register/")
def register():
    # redirect the user to our LoginRadius register URL
    return redirect(LR_AUTH_PAGE.format("register", request.host_url))

loginandreg.png

Once registering and verified in the system, one is redirected to the login page. Can be done by setting our AUTH_ACTION to “login” and our RETURN_URL to our login page.

auth.png

In the dashboard after logging in, the user details are shown show entry into the system.

dashboard.png

To logout of the system, one makes the user access session token void by:

@app.route("/logout/")
def logout():
    access_token = session.get("user_acccess_token")
    if access_token is None:
        return redirect(url_for("login"))

    # invalidate the access token with LoginRadius API
    loginradius.authentication.auth_in_validate_access_token(access_token)
    session.clear()

    return "Successful logout!"

logout.png

My code to the project is here. Happy learning.