-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38ceaff
commit 7952983
Showing
16 changed files
with
364 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
|
||
describe('Auth Controller', () => { | ||
let controller: AuthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
}).compile(); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Controller, Query, Get, Param } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private readonly authService: AuthService){} | ||
@Get('/ids-auth') | ||
async idsAuth(@Query('ticket') ticket: string){ | ||
return { access_token:(await this.authService.authWithIdsTicket(ticket)) }; | ||
} | ||
|
||
@Get('/ids-auth/:wechatSession') | ||
async idsAuthWithWechatSession(@Query('ticket') ticket: string, @Param('wechatSession') wechatSession: string){ | ||
|
||
} | ||
|
||
@Get('/wechat-auth') | ||
async wechatAuth(@Query('code') code: string){ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Document } from 'mongoose'; | ||
|
||
export interface Auth extends Document { | ||
readonly cardnum: string; | ||
readonly name: string; | ||
readonly openId: string; | ||
readonly wechatSession: string, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Module, Injectable } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { ConfigService } from '../config/config.service'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { JwtStrategy } from './jwt.strategy'; | ||
import { AuthController } from './auth.controller'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
import { AuthSchema } from './auth.scheme'; | ||
|
||
const config = new ConfigService() | ||
|
||
@Module({ | ||
imports: [ | ||
PassportModule, | ||
JwtModule.register({ | ||
secret: config.jwtSecret, | ||
signOptions: { expiresIn: '3h' }, | ||
}), | ||
MongooseModule.forFeature([{ name: 'Auth', schema: AuthSchema }]) | ||
], | ||
providers: [AuthService, JwtStrategy], | ||
exports: [ AuthService ], | ||
controllers: [AuthController] | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as mongoose from 'mongoose'; | ||
|
||
export const AuthSchema = new mongoose.Schema({ | ||
name: String, // 姓名 | ||
cardnum: String, // 一卡通号 | ||
openId: String, // 微信公众号 OpenId | ||
wechatSession: String, // 微信绑定过程中会话标识符 | ||
lastAuthTime: Number, // 最近认证时间 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService], | ||
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '../config/config.service'; | ||
import axios from 'axios'; | ||
import { parse as xmlParser } from 'fast-xml-parser'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Auth } from './auth.interface'; | ||
import { Model } from 'mongoose'; | ||
import * as moment from 'moment'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor(private readonly config: ConfigService, | ||
@InjectModel('Auth') private readonly authModel:Model<Auth>, | ||
private readonly jwtService:JwtService | ||
) {} | ||
|
||
async authWithIdsTicket(ticket: string): Promise<string>{ | ||
const validateUrl = `${this.config.idsValidatedUrl}?service=${this.config.idsAuthUrl}&ticket=${ticket}`; | ||
const res = await axios.get(validateUrl); | ||
const rawIdsData = xmlParser(res.data); | ||
try { | ||
let { "cas:uid":cardnum, "cas:cn":name } = rawIdsData["cas:serviceResponse"]["cas:authenticationSuccess"]["cas:attributes"]; | ||
let record = await this.authModel.findOne({ cardnum }); | ||
if(!record){ | ||
record = new this.authModel({ cardnum, name }) | ||
} | ||
record.lastAuthTime = moment().unix(); | ||
await record.save(); | ||
return this.jwtService.sign({ cardnum, name }); | ||
} catch (e) { | ||
console.log(e) | ||
return null | ||
} | ||
} | ||
|
||
async authWithWechatCode(code: string): Promise<string>{ | ||
return 'wechatSession' | ||
} | ||
|
||
async authWithIdsTicketAndWechatSession(ticket: string, wechatSession: string){ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '../config/config.service'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor(private readonly configService: ConfigService) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: configService.jwtSecret, | ||
}); | ||
} | ||
|
||
async validate(payload: any) { | ||
return { cardnum: payload.cardnum }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.