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 * as rest from "./controllers/rest/index";
|
||||
import * as pages from "./controllers/pages/index";
|
||||
import { User } from "./entities/User";
|
||||
import { User } from "./entities/user/User";
|
||||
import "./protocols/GthubProtocol";
|
||||
|
||||
@Configuration({
|
||||
...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 { Configuration } from "@tsed/common";
|
||||
import { Response, Request, NextFunction } from "express";
|
||||
import { Response, Request } from "express";
|
||||
import { Returns } from "@tsed/schema";
|
||||
import { User } from "../../entities/user/User";
|
||||
|
||||
@Controller("/auth")
|
||||
@Scope(ProviderScope.SINGLETON)
|
||||
export class AuthController {
|
||||
@Configuration()
|
||||
private config: Configuration;
|
||||
|
||||
@Get("/github")
|
||||
async githubLogin(
|
||||
@Req() req: Request,
|
||||
@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("/");
|
||||
@Post("/github/login")
|
||||
@Authenticate("github")
|
||||
@Returns(200, User)
|
||||
async githubLogin(@Req() req: Req, @BodyParams("serviceIdentifier") serviceIdentifier: string) {
|
||||
req.query.state = serviceIdentifier;
|
||||
return req.user;
|
||||
}
|
||||
|
||||
@Get("/logout")
|
||||
|
|
|
@ -22,7 +22,9 @@ describe("LinkController", () => {
|
|||
.post("/rest/links")
|
||||
.send({
|
||||
service: "github",
|
||||
serviceIdentifier: username
|
||||
serviceIdentifier: username,
|
||||
text: "some text",
|
||||
iv: "some iv"
|
||||
})
|
||||
.expect(201);
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import { Link } from "../../entities/link/Link";
|
|||
import { CreateLinkDto } from "../../entities/link/CreateLinkDTO";
|
||||
import { UserService } from "../../services/UserService";
|
||||
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")
|
||||
export class LinkController {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { expect, describe, it, afterEach, beforeAll, beforeEach } from "vitest";
|
||||
import { PlatformTest } from "@tsed/common";
|
||||
import { UserController } from "./UserController";
|
||||
import { User } from "../../entities/User";
|
||||
import { User } from "../../entities/user/User";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { Server } from "../../Server";
|
||||
import { SqliteDatasource, sqliteDatasource } from "src/datasources/SqliteDatasource";
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { PathParams } from "@tsed/platform-params";
|
||||
import { Description, Get, Post, Returns, Summary } from "@tsed/schema";
|
||||
import { Controller, Inject } from "@tsed/di";
|
||||
import { User } from "../../entities/User";
|
||||
import { User } from "../../entities/user/User";
|
||||
import { Forbidden } from "@tsed/exceptions";
|
||||
import { UserService } from "../../services/UserService";
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { registerProvider } from "@tsed/di";
|
||||
import { DataSource } from "typeorm";
|
||||
import { Logger } from "@tsed/logger";
|
||||
import { User } from "../entities/User";
|
||||
import { User } from "../entities/user/User";
|
||||
import { Link } from "../entities/link/Link";
|
||||
|
||||
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 {
|
||||
@Property()
|
||||
|
@ -11,4 +11,13 @@ export class CreateLinkDto {
|
|||
@Required()
|
||||
@MaxLength(100)
|
||||
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 { Column, Entity, ManyToOne, PrimaryColumn, JoinColumn, BeforeInsert } from "typeorm";
|
||||
import { User } from "../User";
|
||||
import { User } from "../user/User";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
export type Service = "github";
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { CollectionOf, MaxLength, Property, Required } from "@tsed/schema";
|
||||
import { BeforeInsert, Column, Entity, OneToMany, PrimaryColumn } from "typeorm";
|
||||
import { Link } from "./link/Link";
|
||||
import { Link } from "../link/Link";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
@Entity()
|
|
@ -3,18 +3,17 @@ import { Protocol, OnVerify, OnInstall } from "@tsed/passport";
|
|||
import { Req } from "@tsed/common";
|
||||
import { Inject } from "@tsed/di";
|
||||
import { UserService } from "../services/UserService";
|
||||
import { Strategy as GithubStrategy } from "passport-github";
|
||||
import { SqliteDatasource } from "../datasources/SqliteDatasource";
|
||||
import { Strategy as GithubStrategy, StrategyOptionsWithRequest } from "passport-github";
|
||||
|
||||
@Protocol({
|
||||
@Protocol<StrategyOptionsWithRequest>({
|
||||
name: "github",
|
||||
useStrategy: GithubStrategy,
|
||||
settings: {
|
||||
clientID: process.env.GITHUB_CLIENT_ID || "your-client-id",
|
||||
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"],
|
||||
state: true,
|
||||
state: "true",
|
||||
passReqToCallback: true
|
||||
}
|
||||
})
|
||||
|
@ -22,9 +21,6 @@ export class GithubProtocol implements OnVerify, OnInstall {
|
|||
@Inject()
|
||||
userService: UserService;
|
||||
|
||||
@Inject()
|
||||
sqliteDatasource: SqliteDatasource;
|
||||
|
||||
async $onVerify(@Req() req: Req, accessToken: string, _refreshToken: string, profile: any) {
|
||||
const emails = await this.fetchVerifiedEmails(accessToken);
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Inject, Injectable } from "@tsed/di";
|
||||
import { Forbidden, NotFound } from "@tsed/exceptions";
|
||||
import { SqliteDatasource } from "../datasources/SqliteDatasource";
|
||||
import { User } from "../entities/User";
|
||||
import { User } from "../entities/user/User";
|
||||
import { CreateLinkDto } from "../entities/link/CreateLinkDTO";
|
||||
import { Link } from "../entities/link/Link";
|
||||
import { DataSource, Repository } from "typeorm";
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Inject, Injectable } from "@tsed/di";
|
||||
import { User } from "../entities/User";
|
||||
import { User } from "../entities/user/User";
|
||||
import { SqliteDatasource } from "../datasources/SqliteDatasource";
|
||||
import { DataSource, Repository } from "typeorm";
|
||||
|
||||
|
|
Loading…
Reference in New Issue