Commit 0ba111c1 authored by nanahira's avatar nanahira

first

parent d72f3482
Pipeline #4711 canceled with stages
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
/build
/output
.git*
.dockerignore
Dockerfile
.gitlab-ci.yml
/config.yaml
.idea
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
/build
/dist
/output
/config.yaml
.idea
/install-npm.sh
.git*
/output
/dest
/config.yaml
.idea
.dockerignore
Dockerfile
/src
.eslintrc.js
.prettierrc
webpack.config.js
tsconfig.json
/index.ts
\ No newline at end of file
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
This diff is collapsed.
import {
BadRequestException,
createParamDecorator,
ExecutionContext,
HttpException,
} from '@nestjs/common';
import { Request } from 'express';
import axios, { AxiosResponse } from 'axios';
export interface MyCardUser {
id: number;
username: string;
name: string;
email: string;
password_hash: string;
salt: string;
active: boolean;
admin: boolean;
avatar: string;
locale: string;
registration_ip_address: string;
ip_address: string;
created_at: string;
updated_at: string;
}
export interface FetchMyCardUserOptions {
mycardAccountsUrl: string;
field: string;
}
export async function fetchUserWithToken(
mycardAccountsUrl: string,
token: string,
) {
let authResult: AxiosResponse<MyCardUser>;
try {
authResult = await axios.get<MyCardUser>(`${mycardAccountsUrl}/authUser`, {
responseType: 'json',
validateStatus: (s) => true,
headers: { Authorization: `Bearer ${token}` },
});
} catch (e) {
return null;
}
if (authResult.status >= 400) {
return null;
}
return authResult.data;
}
export const FetchMyCardUser = createParamDecorator(
async (options: FetchMyCardUserOptions, context: ExecutionContext) => {
const _options = {
mycardAccountsUrl:
process.env.MYCARD_ACCOUNTS_URL ||
`https://sapi.moecube.com:444/accounts`,
field: 'sso',
...options,
};
const req = context.switchToHttp().getRequest<Request>();
let token: string;
if (!token && req.headers) {
const authorizationHeader = req.headers['authorization'] as string;
if (authorizationHeader) {
token = authorizationHeader.split(' ').pop();
}
}
if (!token && req.query) {
token = req.query[_options.field] as string;
}
if (!token && req.body) {
token = req.body[_options.field] as string;
}
if (!token) {
return null;
}
return fetchUserWithToken(_options.mycardAccountsUrl, token);
},
);
#!/bin/bash
npm install --save \
lodash
npm install --save-dev \
@types/node \
@types/lodash \
typescript \
'@typescript-eslint/eslint-plugin@^4.28.2' \
'@typescript-eslint/parser@^4.28.2 '\
'eslint@^7.30.0' \
'eslint-config-prettier@^8.3.0' \
'eslint-plugin-prettier@^3.4.0' \
prettier
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "nestjs-mycard-auth",
"version": "1.0.0",
"description": "nest module for mycard accounts",
"main": "dist/index.js",
"scripts": {
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git@git.mycard.moe:mycard/nestjs-mycard-auth.git"
},
"keywords": [
"MyCard",
"Moecube",
"ygopro"
],
"author": "Nanahira",
"license": "AGPL-3.0",
"peerDependencies": {
"@nestjs/common": "^8.0.0 || ^7.0.0"
},
"devDependencies": {
"@nestjs/common": "^8.0.0",
"@types/express": "^4.17.13",
"@types/node": "^16.6.1",
"@typescript-eslint/eslint-plugin": "^4.29.1",
"@typescript-eslint/parser": "^4.29.1",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"prettier": "^2.3.2",
"typescript": "^4.3.5"
},
"dependencies": {
"axios": "^0.21.1"
},
"bugs": {
"url": "https://code.mycard.moe/mycard/nestjs-mycard-auth/issues"
},
"homepage": "https://code.mycard.moe/mycard/nestjs-mycard-auth"
}
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "esnext",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts",
"test/**/*.ts"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment