Search code examples
pythonauthenticationoauth-2.0gramex

How to implement third party authentication in gramex


I am trying to build a simple gramex application. where i need to authorize user from linkedin, outlook, Github & gmail.

Please share some code snippet.


Solution

  • url:
      auth/github:
        pattern: /$YAMLURL/github
        handler: OAuth2
        kwargs:
          # Create app at https://code.gramener.com/admin/applications/
          client_id: 'YOUR_APP_CLIENT_ID'      # https://github.com/settings/connections/applications/
          client_secret: 'YOUR_APP_SECRET_ID'
          authorize:
            url: 'https://github.com/login/oauth/authorize'
            scope: [repo, gist, user]
          access_token:
            url: 'https://github.com/login/oauth/access_token'
            body:
              grant_type: 'authorization_code'
          user_info:
            url: 'https://api.github.com/user'
            headers:
              Authorization: 'Bearer {access_token}'
            redirect:
              query: next
              header: Referer
              url: .
    

    This is the code snippet for Github with OAuth2

    https://docs.github.com/en/developers/apps/building-oauth-apps

    • client_id: Create an app with the OAuth2 provider to get this ID
    • client_secret: Create an app with the OAuth2 provider to get this ID
    • authorize: Authorization endpoint configuration
      • url: Authorization endpoint URL
      • scope: an optional a list of scopes that determine what you can access
    • access_token: Access token endpoint configuration
      • url: Access token endpoint URL
    • user_info: Optional user information API endpoint
      • url: API endpoint to fetch URL
      • headers: optional dict containing HTTP headers to pass to user info URL. e.g.
      • Authorization: 'Bearer {access_token}'. Default: {User-Agent: Gramex/}