Skip to content

Commit f9cfb2c

Browse files
authored
feat: initial service (#13)
* feat: initial service * feat: better error handling * docs: add api documentation * feat: add husky pre-commit * build: update auth-lib version
1 parent e53e8a5 commit f9cfb2c

21 files changed

+6487
-1
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
indent_size = 2
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
end_of_line = lf
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.env.template

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
FUNCTION_NAME=auth-service
3+
NODE_ENV=dev
4+
5+
# Fastify
6+
PORT=3020
7+
HOST=0.0.0.0
8+
9+
# Auth lib
10+
AUTH_URL=http://localhost:8080
11+
KEYCLOAK_REALM=tazama
12+
CERT_PATH=private-key.pem
13+
CLIENT_SECRET=
14+
CLIENT_ID=

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,6 @@ dist
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130130
.pnp.*
131+
132+
# build
133+
build

.husky/pre-commit

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx lint-staged

.npmrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@frmscoe:registry=https://npm.pkg.github.com
2+
//npm.pkg.github.com/:_authToken=${GH_TOKEN}

.prettierignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# Ignore artifacts:
3+
build
4+
coverage
5+
src/models

.prettierrc.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"printWidth": 140,
3+
"tabWidth": 2,
4+
"useTabs": false,
5+
"semi": true,
6+
"singleQuote": true,
7+
"quoteProps": "as-needed",
8+
"trailingComma": "all",
9+
"bracketSpacing": true,
10+
"arrowParens": "always"
11+
}

README.md

+99-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,100 @@
1-
# auth-service
1+
<!-- SPDX-License-Identifier: Apache-2.0 -->
2+
3+
# Auth-Service
4+
5+
<div align="center">
6+
<img alt="GitHub Actions Workflow Status" src="https://img.shields.io/github/actions/workflow/status/frmscoe/auth-service/node.js.yml">
7+
</div>
8+
9+
## Overview
210
Handles credential exchange for a token in Tazama.
11+
12+
#### Setting Up
13+
14+
```sh
15+
git clone https://github.com/frmscoe/auth-service
16+
cd auth-service
17+
```
18+
You then need to configure your environment: a [sample](.env.template) configuration file has been provided and you may adapt that to your environment. Copy it to `.env` and modify as needed:
19+
20+
```sh
21+
cp .env.template .env
22+
```
23+
A [registry](https://github.com/frmscoe/docs) of environment variables is provided to provide more context for what each variable is used for.
24+
25+
##### Additional Variables
26+
27+
| Variable | Purpose | Example
28+
| ------ | ------ | ------ |
29+
| `AUTH_URL` | Base URL where KeyCloak is hosted | `https://keycloak.example.com:8080`
30+
| `KEYCLOAK_REALM` | KeyCloak Realm for Tazama | `tazama`
31+
| `CLIENT_ID` | KeyCloak defined client for auth-lib | `auth-lib-client`
32+
| `CLIENT_SECRET` | The secret of the KeyCloak client | `someClientGeneratedSecret123`
33+
| `CERT_PATH` | The pem file path for signing Tazama tokens | `/path/to/private-key.pem`
34+
35+
#### Build and Start
36+
37+
```sh
38+
npm i
39+
npm run build
40+
npm run start
41+
```
42+
43+
## API
44+
45+
### 1. Auth Login
46+
47+
#### Description
48+
49+
Login using Username and Password to receive a Tazama token.
50+
51+
#### Request
52+
53+
- **Method:** POST
54+
- **URL:** `/v1/auth/login`
55+
- **Headers:**
56+
- `Content-Type: application/json`
57+
- **Body:**
58+
``` JSON
59+
{
60+
"username": "testUser",
61+
"password": "testUserPassword"
62+
}
63+
```
64+
65+
#### Response
66+
67+
- **Status Code:** 200 OK
68+
- **Content-Type:** application/json
69+
- **Body:**
70+
```
71+
eyJhbGciOiJSUz...ukUfoow
72+
```
73+
74+
## Internal Process Flow
75+
76+
### Sequence Diagram
77+
78+
```mermaid
79+
sequenceDiagram
80+
81+
actor Person_OR_Service_APP as Person/Service
82+
actor Operator as Operator
83+
participant Auth_Service as Auth-Service
84+
participant Auth_Provider as KeyCloak
85+
86+
Person_OR_Service_APP ->> Auth_Service: 1. Login request with credentials
87+
Auth_Service ->> Auth_Provider: Exchange credentials by token
88+
alt Invalid Credentials
89+
Auth_Provider ->> Auth_Service: ERR: Invalid Credentials
90+
Auth_Service ->> Person_OR_Service_APP: ERR: 401 Not Authorized
91+
end
92+
Auth_Provider ->> Auth_Service: Receive token in ext format
93+
Auth_Service ->> Person_OR_Service_APP: Token issuance in Tazama format
94+
```
95+
## Troubleshooting
96+
#### npm install
97+
Ensure generated token has read package rights
98+
99+
#### npm build
100+
Ensure that you're on the current LTS version of Node.JS

eslint.config.mjs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
import stylistic from '@stylistic/eslint-plugin';
3+
import tsEslint from '@typescript-eslint/eslint-plugin';
4+
import tsParser from '@typescript-eslint/parser';
5+
import eslintStandard from 'eslint-config-love';
6+
import eslintPluginEslintComments from 'eslint-plugin-eslint-comments';
7+
8+
export default [
9+
{
10+
files: ['**/*.ts'],
11+
plugins: {
12+
...eslintStandard.plugins,
13+
['eslint-comments']: eslintPluginEslintComments,
14+
['@typescript-eslint']: tsEslint,
15+
['@stylistic']: stylistic,
16+
},
17+
languageOptions: {
18+
...eslintStandard.languageOptions,
19+
parser: tsParser,
20+
ecmaVersion: 2022,
21+
sourceType: 'module',
22+
parserOptions: {
23+
project: ['./tsconfig.json'],
24+
},
25+
},
26+
rules: {
27+
...eslintStandard.rules,
28+
...eslintPluginEslintComments.configs.recommended.rules,
29+
'eslint-comments/require-description': ['warn', { 'ignore': [] }],
30+
'eslint-comments/disable-enable-pair': 'warn',
31+
'no-console': 'warn',
32+
'@typescript-eslint/restrict-template-expressions': 'error',
33+
'@typescript-eslint/no-non-null-assertion': 'off',
34+
'@typescript-eslint/strict-boolean-expressions': 'off',
35+
'@typescript-eslint/no-explicit-any': 'error',
36+
'@typescript-eslint/no-floating-promises': 'off',
37+
'@typescript-eslint/no-var-requires': 'off',
38+
'@typescript-eslint/no-use-before-define': 'off',
39+
'@typescript-eslint/prefer-optional-chain': 'off',
40+
'@stylistic/indent': ['error', 2],
41+
'@stylistic/semi': ['warn', 'always'],
42+
'@stylistic/quotes': ['error', 'single'],
43+
'@stylistic/quote-props': ['warn', 'as-needed'],
44+
'@stylistic/arrow-parens': ["warn", "always"],
45+
},
46+
ignores: [
47+
'**/coverage/**',
48+
'**/build/**',
49+
'**/node_modules/**',
50+
'**/__tests__/**',
51+
'**/jest.config.ts',
52+
'**/cluster-setup.ts',
53+
],
54+
}
55+
];

0 commit comments

Comments
 (0)