# mac-authorization

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

Latest version **1.0.12** (published 2020-07-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install mac-authorization
pnpm add mac-authorization
yarn add mac-authorization
bun add mac-authorization
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2020-07-02 |
| First published | 2020-07-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 20 |
| Unpacked size | 50.1 KB |
| Known vulnerabilities | 0 (+11 in 6 direct dependencies) |
| Install scripts | no |
| Maintainers | vanishdark |

## Links

- npm: https://www.npmjs.com/package/mac-authorization
- Repository: http://10.172.5.51/mac/backend-folder/microservices/authorization
- npm.io page: https://npm.io/package/mac-authorization

## Dependencies (20)

- [rxjs](https://npm.io/package/rxjs.md) ^6.5.4
- [redis](https://npm.io/package/redis.md) ^3.0.2
- [dotenv](https://npm.io/package/dotenv.md) ^8.2.0
- [rimraf](https://npm.io/package/rimraf.md) ^3.0.0
- [tunnel](https://npm.io/package/tunnel.md) 0.0.6
- [mongoose](https://npm.io/package/mongoose.md) ^5.9.14
- [passport](https://npm.io/package/passport.md) ^0.4.0
- [cross-env](https://npm.io/package/cross-env.md) ^7.0.2
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) ^6.11.8
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [passport-jwt](https://npm.io/package/passport-jwt.md) ^4.0.0
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) ^6.11.8
- [passport-oauth](https://npm.io/package/passport-oauth.md) ^1.0.0
- [passport-oauth2](https://npm.io/package/passport-oauth2.md) ^1.5.0
- [@nestjs/mongoose](https://npm.io/package/@nestjs/mongoose.md) ^7.0.1
- [@nestjs/passport](https://npm.io/package/@nestjs/passport.md) ^6.2.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [https-proxy-agent](https://npm.io/package/https-proxy-agent.md) ^5.0.0
- [@nestjs/microservices](https://npm.io/package/@nestjs/microservices.md) ^6.11.8
- [@nestjs/platform-express](https://npm.io/package/@nestjs/platform-express.md) ^6.11.8

## Recent versions

- 1.0.12 (latest) — 2020-07-02

## README

# MaC - Authorization
This is the authorization implementation with Passport oAuth2. Work with SSO.

## Table of Contents

- 1.[ Required Software ](#required-software)
  - 1.1[ NodeJs Installation ](#11-node-installation)
  - 1.2 [ NestJs Installation ](#12-nestjs-installation)
- 2.[ Git ](#git)
- 3.[ Configuration ](#configuration)
  - 3.1[ Environment Files ](#31-environment-files)
    - 3.1.1[ Environment Variables](#311-environment-variables)
  - 3.2[ Set environment ](#32-set-environments)
- 4 [ Endpoints ](#endpoints)
  - 4.1 [External Endpoints](#41-external-endpoints)

## Required Software

![npm (tag)](https://img.shields.io/badge/Node-v10.15.3-blue?style=for-the-badge&logo=node.js)
![npm (tag)](https://img.shields.io/badge/NestJs-v6.7.2-blue?style=for-the-badge&logo=node.js)

### 1.1 Node Installation
For node installation go to 

<a href="https://nodejs.org/en/download/">![npm (tag)](https://img.shields.io/badge/Node-download-brightgreen?style=for-the-badge&logo=node.js)</a>

Download the current version which are the one we use. 

Follow the steps from this guides bellow to install node.

<a href="https://treehouse.github.io/installation-guides/windows/node-windows.html">![npm (tag)](https://img.shields.io/badge/Windows-installation-orange?style=for-the-badge&logo=windows&logoColor=#ffffff)</a>
<a href="https://tecadmin.net/install-latest-nodejs-npm-on-ubuntu/">![npm (tag)](https://img.shields.io/badge/Linux-installation-orange?style=for-the-badge&logo=linux&logoColor=#ffffff)</a>
<a href="https://nodesource.com/blog/installing-nodejs-tutorial-mac-os-x/">![npm (tag)](https://img.shields.io/badge/Mac-installation-orange?style=for-the-badge&logo=apple&logoColor=#fffffff)</a>

### 1.2 NestJs Installation

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

```shell script
$ npm install -g @nestjs/cli
```

After the installation finish check if your system recognize nest.
```shell script
$ nest --version
```

Should output something like this
```shell script
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.

<a href="https://www.atlassian.com/git/tutorials/install-git">![npm (tag)](https://img.shields.io/badge/Git-installation-orange?style=for-the-badge)</a>

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


> **With http**
> ```shell script
> $ 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**
> ```shell script
> $ 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**
>```shell script
> 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
```shell script
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.

```shell script
cross-env NODE_ENV=development && nest start --watch
```
or use the pre-configured script
```shell script
npm run start:dev
```

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

```shell script
cross-env NODE_ENV=production && node dist/main
```
or use the pre-configured script
```shell 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.
```typescript
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.

<table>
<thead>
<th>Name</th>
<th>Description</th>
<th>Endpoint</th>
</thead>
<tbody>
<tr>
<td>
Login
</td>
<td>
return url with login endpoint to redirect
</td>
<td>
yourauthorizationurl.com/oauth/login
</td>
</tr>
<tr>
<td>
Verify Token
</td>
<td>
return a boolean with token validity
</td>
<td>
yourauthorizationurl.com/oauth/verifyToken
</td>
</tr>
<tr>
<td>
Get User Info
</td>
<td>
return a jwt token with user info
</td>
<td>
yourauthorizationurl.com/oauth/getUserInfo
</td>
</tr>
<tr>
<td>
Logout
</td>
<td>
return url with logout endpoint to redirect
</td>
<td>
yourauthorizationurl.com/oauth/logout
</td>
</tr>
</tbody>
</table>

Angular implementation example

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

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

**Service**
```typescript
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**
```typescript
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**

<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="100" alt="Nest Logo" /></a>

<a href="https://www.mongodb.com/" target="blank"><img src="https://webassets.mongodb.com/_com_assets/cms/MongoDB_Logo_FullColorBlack_RGB-4td3yuxzjs.png" width="150" alt="MongoDB Logo" /></a>

---
_Source: https://npm.io/package/mac-authorization · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
