Code Monkey home page Code Monkey logo

cognises-flask's Introduction

Flask Cognises: AWS Cognito Group Based Authorization

GitHub license Build Status CodeFactor PRs Welcome PyPI

This package gives the developer fine grain control over their users through Group based Permission Using AWS Cognito, including python middlewares(decorators) called login_check, for checking if the user is logged in through AWS Cognito and another middleware called permission_required which checks the route access permissions for that user .

Note: This package is built essentially for usage within a Flask application.

Installation

The package can be installed using the pip install command:

pip install cognises

How To Setup

1) Create a group within a user pool

The create_update function allows:

  • creation of new iam role and a new cognito user pool group and links the user pool group to the newly created iam role.
  • updation of the role policy of the role already linked to a user group

The function takes 4 arguments:

  • group_detail [Required] - It is a json object that contains details for one or more groups. Each group has a created attribute which takes two values: -- true: Specifies that the group is already created and implements the update part of the function which updates the role policy. -- false: Specifies that a new group has to be created and implements the creation part of the function to create a new iam role and a new cognito user pool group.

The format of the json object is:

[
  {
    "group_name": "Group1",
    "group_policy": {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "Stmt1524591948858",
          "Action": "cognito-idp:*",
          "Effect": "Allow",
          "Resource": "arn:aws:cognito-idp:us-east-1:userid:userpool/pool_id"
        }
      ]
    },
    "created": "false",
    "allowed_functions": ["protected", "admin_panel", "update_data"]
  },
  {
    "group_name": "String",
    "group_policy": {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "Stmt1524591948858",
          "Action": "cognito-idp:*",
          "Effect": "Allow",
          "Resource": "arn:aws:cognito-idp:us-east-1:userid:userpool/pool_id"
        }
      ]
    },
    "created": "false",
    "allowed_functions": ["public", "view_data"]
  }
]

Note: The group_policy is the aws policy for the role attached to that group. Refer to the following link to generate your fine-controlled policies: AWS Policy Generator

  • iam_client [Required] - The boto3 iam client.

    python boto3.client('iam', 'aws_region', 'aws_access_key_id', 'aws_secret_access_key')
  • cognito_client [Required] - The boto3 aws cognito-idp client.

    python boto3.client('cognito-idp', 'aws_region', 'aws_access_key_id', 'aws_secret_access_key')
  • cognito_pool_id - The aws cognito user pool id. It is required when a new group has to be created. In case of updating already existing group, this argument is not required.

Example usage:

import boto3
import os
from flask import json
from cognises import create_update

iam_client = boto3.client('iam', 'aws_region', 'aws_access_key_id', 'aws_secret_access_key')
cognito_client = boto3.client('cognito-idp', 'aws_region', 'aws_access_key_id', 'aws_secret_access_key')

script_dir = os.path.dirname(__file__)
file_name = "group_detail.json"
abs_file_path = os.path.join(script_dir, file_name)
data = json.load(open(abs_file_path))

create_update(data, iam_client, cognito_client, 'cognito_pool_id')

2) login_check decorator

This decorator checks if the user already has a valid AWS Cognito token or not to access the route, and works much like @login_required decorator in Flask. It takes 2 arguments:

  • cognito_pool_region [Required] - The region in which the cognito user pool is created in
  • cognito_pool_id [Required] - The id of the cognito user pool

Example usage:

from cognises import login_check

@app.route('/protected', methods=['GET','POST'])
@login_check('cognito_pool_region', 'cognito_pool_id')
def protected(response):
	if response['status'] == 200:
		return response['user_email']
	else:
		return response['message']

3) permission_required decorator

This decorator checks whether the user can access the route. It is used along with the login_check decorator and checks whether the route is present in the allowed functions for the cognito user pool group to which the user belongs and restricts the access for the user if the route in not present in it. It takes the group_detail argument which is json object that contains details for one or more groups. It has the same structure described in point 1.

Example usage:

from cognises import login_check, permission_required

@app.route('/protected', methods=['GET','POST'])
@login_check('cognito_pool_region', 'cognito_pool_id')
@permission_required(group_details)
def protected(response):
	if response['status'] == 200:
		return response['user_email']
	else:
		return response['message']

cognises-flask's People

Contributors

chaitanyya avatar shaliniaggarwal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

jrlaforge

cognises-flask's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.