All Projects → plotlabs → cognises-flask

plotlabs / cognises-flask

Licence: MIT license
Flask Cognises: AWS Cognito group based authorization with user management

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to cognises-flask

terraform-aws-cognito-user-pool
Terraform module to create Amazon Cognito User Pools, configure its attributes and resources such as app clients, domain, resource servers. Amazon Cognito User Pools provide a secure user directory that scales to hundreds of millions of users.
Stars: ✭ 65 (+306.25%)
Mutual labels:  aws-cognito, cognito-user-pool
aws-sdk-net-extensions-cognito
An extension library to assist in the Amazon Cognito User Pools authentication process
Stars: ✭ 80 (+400%)
Mutual labels:  aws-cognito, cognito-user-pool
futils
Utilities for generic functional programming
Stars: ✭ 21 (+31.25%)
Mutual labels:  decorators
resty
A Node.js framework
Stars: ✭ 20 (+25%)
Mutual labels:  decorators
vue-corator
this is vue decorator utils
Stars: ✭ 33 (+106.25%)
Mutual labels:  decorators
terraform-aws-cognito-user-pool
A Terraform module to create and manage Cognito User Pools (Simple and Secure User Sign-Up, Sign-In, and Access Control) on Amazon Web Services (AWS). https://aws.amazon.com/cognito
Stars: ✭ 46 (+187.5%)
Mutual labels:  cognito-user-pool
aws-node-custom-user-pool
Serverless AWS Cognito Custom User Pool Example
Stars: ✭ 15 (-6.25%)
Mutual labels:  cognito-user-pool
realar
5 kB Advanced state manager for React
Stars: ✭ 41 (+156.25%)
Mutual labels:  decorators
holochrome
Use your IAM role (from instance metadata) to open the AWS console
Stars: ✭ 102 (+537.5%)
Mutual labels:  iam-role
invoiceless
Serverless backend for sending simple recurring invoices
Stars: ✭ 44 (+175%)
Mutual labels:  boto3
koa-smart
A framework base on Koajs2 with Decorator, Params checker and a base of modules (cors, bodyparser, compress, I18n, etc…) to let you develop smart api easily
Stars: ✭ 31 (+93.75%)
Mutual labels:  decorators
typijs
The Angular CMS Framework for building fully-featured SPA sites powered by NodeJS and MongoDB with TypeScript
Stars: ✭ 141 (+781.25%)
Mutual labels:  decorators
type-arango
🥑 TypeArango manages ArangoDB collections, documents, relations and routes by taking advantage of TypeScript typings.
Stars: ✭ 55 (+243.75%)
Mutual labels:  decorators
drape
Drape – Reincarnation of Draper for Rails 5
Stars: ✭ 57 (+256.25%)
Mutual labels:  decorators
mocha-allure2-example
Allure 2 Mocha examples
Stars: ✭ 18 (+12.5%)
Mutual labels:  decorators
cognito-to-dynamodb-lambda
Copy newly-confirmed users from Cognito to DynamoDB
Stars: ✭ 68 (+325%)
Mutual labels:  aws-cognito
TvrboReact
Dream starter project: React, Redux, React Router, Webpack
Stars: ✭ 13 (-18.75%)
Mutual labels:  decorators
lifemanager
⏱ 한 일을 기록하면 시각화 해서 보여주는 웹 앱⏱
Stars: ✭ 85 (+431.25%)
Mutual labels:  aws-cognito
soap-typescript
SOAP decorators for creating wsdl's and annotating services to provide metadata for node-soap
Stars: ✭ 20 (+25%)
Mutual labels:  decorators
stupid-python-tricks
Stupid Python tricks.
Stars: ✭ 112 (+600%)
Mutual labels:  decorators

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']
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].