Skip to main content

A no BS auth package to add google login to your Restful APIs

Project description

No BullShit Python Social Auth

Inspiration:

Building SPAs and RestFul APIs in python frameworks like Django/ FastAPI/ Flask etc are a delight unless we encounter adding social login. There is pretty good support for social login in Django for Templated apps using Social-Core but this tends to be sticky to the templated applications and becomes confusing to provide a plain no Bull Shit approach to add a basic Google Social Login to the API,

This is a fairly simple package with it's inital version supporting only Google Social Login since it's only thing I wanted to add in the APP I was building but it's open for contributions and feedback from the community to improve and add other providers.

To use it,

from nbs_social_auth.google import GoogleOAuth2

# Intialize the Google OAuth with the Credentials 

google_auth = GoogleOAuth2(GOOGLE_CLIENT_ID=<GOOGLE_CLIENT_ID>, GOOGLE_CLIENT_SECRET=<GOOGLE_OAUTH2_CLIENT_SECRET>,GOOGLE_REDIRECT_URI=<GOOGLE_CLIENT_SECRET>)


# Now we open up and endpoint to grab the AUTH URL on the APP
# In future we can add multiple providers and then send the AUTH URL based on a parameter 

@router.get("/social-auth-init-url")
def get_social_auth_url(request):


    return google_auth.get_auth_url()

Now the client recieves the auth URL and goes there to grab the auth credentials and we usually want it to redirect to a Frontned Page where we grab the auth token and then call the next API -> which would exchange the token with a JWT that is used across the API for authentication of the User

@router.get("/check-social-auth-code")
def check_social_auth_code(request,code:str):
    try:
        social_user = google_auth.validate_code(code)
        email = social_user.get('email')

        # Register the User here / set provider / avatar-url or other parameters
        # Generate the token as per your logic here
        token =  jwt.encode({'email': email},SECRET, algorithm=algorithm)
        
        # Return the token, save on client side and use for subsequent authenticated requests 
        
    except Exception as e:
        # Handle the Error and pass a meaningful error message to the client 

Drop issues here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbs_social_auth-0.1.2.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nbs_social_auth-0.1.2-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file nbs_social_auth-0.1.2.tar.gz.

File metadata

  • Download URL: nbs_social_auth-0.1.2.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.0 Darwin/21.6.0

File hashes

Hashes for nbs_social_auth-0.1.2.tar.gz
Algorithm Hash digest
SHA256 50fa0bb14b92bc42b0f74a897de9cb352f742d223e4ed6350bbf7ec4fc1c4c09
MD5 a96d42d89a736506f71f765f18884a41
BLAKE2b-256 f3b7a66b47a6554e0b58264ee698851272a2d076f871a720536716ebe90a9e6f

See more details on using hashes here.

File details

Details for the file nbs_social_auth-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: nbs_social_auth-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.0 CPython/3.10.0 Darwin/21.6.0

File hashes

Hashes for nbs_social_auth-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8f78cf0ee16be7181d5436746a4dc45dc48bbc5b79135b6be60a61a535620eb
MD5 46fbb505867a4ef21859ae1ab7d55f18
BLAKE2b-256 f7c52a0319f3d0ad78031c3e80dc0ba56fe2ce2a07c93dc4e3f60632cdafa326

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page