various fixes for github auth, get passing test coverage
This commit is contained in:
parent
64edba3cf8
commit
d98f589031
|
@ -8,7 +8,8 @@ import "@tsed/passport";
|
||||||
import { config } from "./config/index";
|
import { config } from "./config/index";
|
||||||
import * as rest from "./controllers/rest/index";
|
import * as rest from "./controllers/rest/index";
|
||||||
import * as pages from "./controllers/pages/index";
|
import * as pages from "./controllers/pages/index";
|
||||||
import { User } from "./entities/User";
|
import { User } from "./entities/user/User";
|
||||||
|
import "./protocols/GthubProtocol";
|
||||||
|
|
||||||
@Configuration({
|
@Configuration({
|
||||||
...config,
|
...config,
|
||||||
|
|
|
@ -1,40 +1,18 @@
|
||||||
import { Controller, Get, Req, Res, Next, QueryParams } from "@tsed/common";
|
import { Controller, Get, Req, Res, Scope, ProviderScope, Post, BodyParams } from "@tsed/common";
|
||||||
import { Authenticate } from "@tsed/passport";
|
import { Authenticate } from "@tsed/passport";
|
||||||
import { Configuration } from "@tsed/common";
|
import { Response, Request } from "express";
|
||||||
import { Response, Request, NextFunction } from "express";
|
import { Returns } from "@tsed/schema";
|
||||||
|
import { User } from "../../entities/user/User";
|
||||||
|
|
||||||
@Controller("/auth")
|
@Controller("/auth")
|
||||||
|
@Scope(ProviderScope.SINGLETON)
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
@Configuration()
|
@Post("/github/login")
|
||||||
private config: Configuration;
|
@Authenticate("github")
|
||||||
|
@Returns(200, User)
|
||||||
@Get("/github")
|
async githubLogin(@Req() req: Req, @BodyParams("serviceIdentifier") serviceIdentifier: string) {
|
||||||
async githubLogin(
|
req.query.state = serviceIdentifier;
|
||||||
@Req() req: Request,
|
return req.user;
|
||||||
@Res() res: Response,
|
|
||||||
@Next() next: NextFunction,
|
|
||||||
@QueryParams("serviceIdentifier") serviceIdentifier: string
|
|
||||||
) {
|
|
||||||
if (!serviceIdentifier) {
|
|
||||||
res.status(400).send("serviceIdentifier is required");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initiate authentication with the 'state' parameter
|
|
||||||
return Authenticate("github", {
|
|
||||||
scope: ["user:email"],
|
|
||||||
state: serviceIdentifier
|
|
||||||
})(req, res, next);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get("/github/callback")
|
|
||||||
@Authenticate("github", { failureRedirect: "/login" })
|
|
||||||
async githubCallback(@Req() req: Request, @Res() res: Response) {
|
|
||||||
// Authentication was successful
|
|
||||||
// You can redirect the user to a specific page or return user info
|
|
||||||
|
|
||||||
// Example: Redirect to the home page
|
|
||||||
res.redirect("/");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get("/logout")
|
@Get("/logout")
|
||||||
|
|
|
@ -22,7 +22,9 @@ describe("LinkController", () => {
|
||||||
.post("/rest/links")
|
.post("/rest/links")
|
||||||
.send({
|
.send({
|
||||||
service: "github",
|
service: "github",
|
||||||
serviceIdentifier: username
|
serviceIdentifier: username,
|
||||||
|
text: "some text",
|
||||||
|
iv: "some iv"
|
||||||
})
|
})
|
||||||
.expect(201);
|
.expect(201);
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { Link } from "../../entities/link/Link";
|
||||||
import { CreateLinkDto } from "../../entities/link/CreateLinkDTO";
|
import { CreateLinkDto } from "../../entities/link/CreateLinkDTO";
|
||||||
import { UserService } from "../../services/UserService";
|
import { UserService } from "../../services/UserService";
|
||||||
import { LinkService } from "../../services/LinkService"; // Create a new service for Link operations
|
import { LinkService } from "../../services/LinkService"; // Create a new service for Link operations
|
||||||
import { User } from "../../entities/User";
|
import { User } from "../../entities/user/User";
|
||||||
|
|
||||||
@Controller("/links")
|
@Controller("/links")
|
||||||
export class LinkController {
|
export class LinkController {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { expect, describe, it, afterEach, beforeAll, beforeEach } from "vitest";
|
import { expect, describe, it, afterEach, beforeAll, beforeEach } from "vitest";
|
||||||
import { PlatformTest } from "@tsed/common";
|
import { PlatformTest } from "@tsed/common";
|
||||||
import { UserController } from "./UserController";
|
import { UserController } from "./UserController";
|
||||||
import { User } from "../../entities/User";
|
import { User } from "../../entities/user/User";
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
import { Server } from "../../Server";
|
import { Server } from "../../Server";
|
||||||
import { SqliteDatasource, sqliteDatasource } from "src/datasources/SqliteDatasource";
|
import { SqliteDatasource, sqliteDatasource } from "src/datasources/SqliteDatasource";
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { PathParams } from "@tsed/platform-params";
|
import { PathParams } from "@tsed/platform-params";
|
||||||
import { Description, Get, Post, Returns, Summary } from "@tsed/schema";
|
import { Description, Get, Post, Returns, Summary } from "@tsed/schema";
|
||||||
import { Controller, Inject } from "@tsed/di";
|
import { Controller, Inject } from "@tsed/di";
|
||||||
import { User } from "../../entities/User";
|
import { User } from "../../entities/user/User";
|
||||||
import { Forbidden } from "@tsed/exceptions";
|
import { Forbidden } from "@tsed/exceptions";
|
||||||
import { UserService } from "../../services/UserService";
|
import { UserService } from "../../services/UserService";
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { registerProvider } from "@tsed/di";
|
import { registerProvider } from "@tsed/di";
|
||||||
import { DataSource } from "typeorm";
|
import { DataSource } from "typeorm";
|
||||||
import { Logger } from "@tsed/logger";
|
import { Logger } from "@tsed/logger";
|
||||||
import { User } from "../entities/User";
|
import { User } from "../entities/user/User";
|
||||||
import { Link } from "../entities/link/Link";
|
import { Link } from "../entities/link/Link";
|
||||||
|
|
||||||
export const SqliteDatasource = Symbol.for("SqliteDatasource");
|
export const SqliteDatasource = Symbol.for("SqliteDatasource");
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Property, Required, MaxLength, Enum } from "@tsed/schema";
|
import { Property, Required, MaxLength, Enum, Description } from "@tsed/schema";
|
||||||
|
|
||||||
export class CreateLinkDto {
|
export class CreateLinkDto {
|
||||||
@Property()
|
@Property()
|
||||||
|
@ -11,4 +11,13 @@ export class CreateLinkDto {
|
||||||
@Required()
|
@Required()
|
||||||
@MaxLength(100)
|
@MaxLength(100)
|
||||||
serviceIdentifier: string;
|
serviceIdentifier: string;
|
||||||
|
|
||||||
|
@Property()
|
||||||
|
@Required()
|
||||||
|
text: string;
|
||||||
|
|
||||||
|
@Property()
|
||||||
|
@Required()
|
||||||
|
@Description("The iv used to encrypt the text")
|
||||||
|
iv: string;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Enum, MaxLength, Property, Required } from "@tsed/schema";
|
import { Enum, MaxLength, Property, Required } from "@tsed/schema";
|
||||||
import { Column, Entity, ManyToOne, PrimaryColumn, JoinColumn, BeforeInsert } from "typeorm";
|
import { Column, Entity, ManyToOne, PrimaryColumn, JoinColumn, BeforeInsert } from "typeorm";
|
||||||
import { User } from "../User";
|
import { User } from "../user/User";
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
|
|
||||||
export type Service = "github";
|
export type Service = "github";
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { CollectionOf, MaxLength, Property, Required } from "@tsed/schema";
|
import { CollectionOf, MaxLength, Property, Required } from "@tsed/schema";
|
||||||
import { BeforeInsert, Column, Entity, OneToMany, PrimaryColumn } from "typeorm";
|
import { BeforeInsert, Column, Entity, OneToMany, PrimaryColumn } from "typeorm";
|
||||||
import { Link } from "./link/Link";
|
import { Link } from "../link/Link";
|
||||||
import { v4 as uuidv4 } from "uuid";
|
import { v4 as uuidv4 } from "uuid";
|
||||||
|
|
||||||
@Entity()
|
@Entity()
|
|
@ -3,18 +3,17 @@ import { Protocol, OnVerify, OnInstall } from "@tsed/passport";
|
||||||
import { Req } from "@tsed/common";
|
import { Req } from "@tsed/common";
|
||||||
import { Inject } from "@tsed/di";
|
import { Inject } from "@tsed/di";
|
||||||
import { UserService } from "../services/UserService";
|
import { UserService } from "../services/UserService";
|
||||||
import { Strategy as GithubStrategy } from "passport-github";
|
import { Strategy as GithubStrategy, StrategyOptionsWithRequest } from "passport-github";
|
||||||
import { SqliteDatasource } from "../datasources/SqliteDatasource";
|
|
||||||
|
|
||||||
@Protocol({
|
@Protocol<StrategyOptionsWithRequest>({
|
||||||
name: "github",
|
name: "github",
|
||||||
useStrategy: GithubStrategy,
|
useStrategy: GithubStrategy,
|
||||||
settings: {
|
settings: {
|
||||||
clientID: process.env.GITHUB_CLIENT_ID || "your-client-id",
|
clientID: process.env.GITHUB_CLIENT_ID || "your-client-id",
|
||||||
clientSecret: process.env.GITHUB_CLIENT_SECRET || "your-client-secret",
|
clientSecret: process.env.GITHUB_CLIENT_SECRET || "your-client-secret",
|
||||||
callbackURL: "http://localhost:8080/auth/github/callback",
|
callbackURL: "http://localhost:8083/auth/github/callback",
|
||||||
scope: ["user:email"],
|
scope: ["user:email"],
|
||||||
state: true,
|
state: "true",
|
||||||
passReqToCallback: true
|
passReqToCallback: true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -22,9 +21,6 @@ export class GithubProtocol implements OnVerify, OnInstall {
|
||||||
@Inject()
|
@Inject()
|
||||||
userService: UserService;
|
userService: UserService;
|
||||||
|
|
||||||
@Inject()
|
|
||||||
sqliteDatasource: SqliteDatasource;
|
|
||||||
|
|
||||||
async $onVerify(@Req() req: Req, accessToken: string, _refreshToken: string, profile: any) {
|
async $onVerify(@Req() req: Req, accessToken: string, _refreshToken: string, profile: any) {
|
||||||
const emails = await this.fetchVerifiedEmails(accessToken);
|
const emails = await this.fetchVerifiedEmails(accessToken);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Inject, Injectable } from "@tsed/di";
|
import { Inject, Injectable } from "@tsed/di";
|
||||||
import { Forbidden, NotFound } from "@tsed/exceptions";
|
import { Forbidden, NotFound } from "@tsed/exceptions";
|
||||||
import { SqliteDatasource } from "../datasources/SqliteDatasource";
|
import { SqliteDatasource } from "../datasources/SqliteDatasource";
|
||||||
import { User } from "../entities/User";
|
import { User } from "../entities/user/User";
|
||||||
import { CreateLinkDto } from "../entities/link/CreateLinkDTO";
|
import { CreateLinkDto } from "../entities/link/CreateLinkDTO";
|
||||||
import { Link } from "../entities/link/Link";
|
import { Link } from "../entities/link/Link";
|
||||||
import { DataSource, Repository } from "typeorm";
|
import { DataSource, Repository } from "typeorm";
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Inject, Injectable } from "@tsed/di";
|
import { Inject, Injectable } from "@tsed/di";
|
||||||
import { User } from "../entities/User";
|
import { User } from "../entities/user/User";
|
||||||
import { SqliteDatasource } from "../datasources/SqliteDatasource";
|
import { SqliteDatasource } from "../datasources/SqliteDatasource";
|
||||||
import { DataSource, Repository } from "typeorm";
|
import { DataSource, Repository } from "typeorm";
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue