1.0.12 • Published 4 years ago

mac-authorization v1.0.12

Weekly downloads
5
License
MIT
Repository
-
Last release
4 years ago

MaC - Authorization

This is the authorization implementation with Passport oAuth2. Work with SSO.

Table of Contents

Required Software

npm (tag) npm (tag)

1.1 Node Installation

For node installation go to

npm (tag)

Download the current version which are the one we use.

Follow the steps from this guides bellow to install node.

npm (tag) npm (tag) npm (tag)

1.2 NestJs Installation

The Installation of NestJs is really simple. After install NodeJs you only need run the follow command in your terminal.

$ npm install -g @nestjs/cli

After the installation finish check if your system recognize nest.

$ nest --version

Should output something like this

6.11.3

Now that all required software are installed lets move on into the git clone and git submodule.

Git

First you will need to install git into your system if you don't know how go to guide bellow and you have how to install in 3 major OS.

npm (tag)

After install git into your system you will need to clone the repo to your system. Follow the next steps for success.

With http

$ git clone http://10.172.5.51/mac/backend-folder/backend.git backend

Will ask you to put your credentials. username first and password after. this credentials are the same you use to login into gitlab.


With SSH

$ git clone git@10.172.5.51:mac/backend-folder/backend.git backend

This one will not ask for nothing. If throw error just check your profile permissions.


Fork Project

http://10.172.5.51/mac/backend-folder/microservices/authorization/-/forks/new

Access to this url to fork the project and add your namespace.

Configuration

You need to follow some steps before start using this microservice.

3.1 Environment files

The project have 2 environment files, development and production, each one with different configurations. development.env file has all dev endpoints and localhost endpoints. If you wanna chane any variable or endpoint for local test use this file, otherwise use production or create one for other environment.

3.1.1 Environment Variables

CRYPTO_KEY = <JWT KEY>
SSO_AUTH_SCOPE = <SCOPE>
SSO_CLIENT_ID = <CLIENT ID>
SSO_CLIENT_SECRET = <CLIENT SECRET>
SSO_CALLBACK_URL = <URL FOR YOUR APP ENDPOINT http://www.example.com/oauth/token>
SSO_LOGIN_URI = <URL FOR YOUR APP LOGIN ENDPOINT http://www.example.com/oauth/startAuth>
SSO_LOGOUT_URI = <URL FOR YOUR FRONT END ENDPOINT http://www.examplefrontend.com>
SSO_REDIRECT = <URL FOR YOUR FRONT END LOGIC PAGE http://www.examplefrontend.com/auth/login>

To run the microservice with development environment you need run the follow command.

cross-env NODE_ENV=development && nest start --watch

or use the pre-configured script

npm run start:dev

To run the microservice with production environment you need run the follow command.

cross-env NODE_ENV=production && node dist/main

or use the pre-configured script

npm run start:prod

3.2 Set environments

To add new environment files into the project, add a new file in environment folder with like {name}.env where name is the environment name and env is the file extension, do not change or use other extension.

If for some reason the microservice don't start up your new environment go to config folder and open config.module file.

Add the new environment name on this line.

useValue: new ConfigService(`src/environments/${process.env.NODE_ENV || 'development' || 'production' || 'your_env_name'}.env`)

EndPoints

Below are listed all endpoints to this service. Some are not for external use, they are only for sso connect with our application.

4.1 External Endpoints

The external endpoints are the only endpoints you should use with your client. Follow the list.

Angular implementation example

First you need to call the login service to get the server endpoint to start authentication

Component

this.service.login().subscribe(value =>{window.open(value.url, '_self')});

Service

login(): Observable<any> {
    return this.http.get<any>(endpointurl/oauth/login);
}

You will be redirect to SSO authentication. After successful login you will be redirect to the next endpoint which should have your login for example:

Logic Componet

this.service.verifyToken(token).pipe(
    switchMap(value => {
        if (value) {
            return this.service.getUserInfo(token);
        }  
}))
.subscribe(value => {
    if (value) {
        Storage.setLocalMemory('userinfo', value.jwt);    
    }
});

At this point you should have the cookie with access token injected.

This logic first verify token if it is valid, if not we dont go get userInfo. You can skip your verify token and go into getUserInfo since both validate tokens.

To implement logout use the same login logic with window.open.


Powered by