GithubHelp home page GithubHelp logo

Comments (4)

github-actions avatar github-actions commented on June 13, 2024

Hello there kbublitz 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

from tsoa.

github-actions avatar github-actions commented on June 13, 2024

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

from tsoa.

kbublitz avatar kbublitz commented on June 13, 2024

Now before my issue gets closed without any response, let me give a short update. I now implemented it the way I suggested in my initial question. It looks like the following snippet and seems to work well for my use case.

export async function expressAuthentication(
    request: express.Request,
    securityName: string,
    scopes?: string[],
): Promise<any> {
    switch (securityName) {
        case "authToken":
            // This will use jsonwebtoken to verify a jwt and resolve with a user object like { userName: "John Doe", "permissions": ["readPrivateFiles", "readPublicFiles"] }
            return await evaluateToken(request, scopes);

        case "authTokenOrPublic":
            return await maybeEvaluateToken(request, scopes);
    }

    return Promise.reject("invalid-authentication-method");
}

async function maybeEvaluateToken(request, scopes) {
    try {
        return await evaluateToken(request, scopes);
    } catch (error) {
        return { userName: "public", permissions: ["readPublicFiles"] };
    }
}

async function evaluateToken(request, scopes) {
    // check jwt and so on ...
    return realUserObject;
}

The controller is then annotated only with @Security("authTokenOrPublic").

I'll leave the issue open until it closes automatically, still any feedback is welcome.

from tsoa.

daxadal avatar daxadal commented on June 13, 2024

I tried to implement the same behaviour, and I used the scopes for that. I included an optional scope, and i only throw and Unauthorized error when the optional scope is not present. Then, you'll only need the authToken security method. He's an expample code based on yours:

export async function expressAuthentication(
  request: express.Request,
  securityName: string,
  scopes?: string[]
): Promise<any> {
  switch (securityName) {
    case "authToken":
      // This will use jsonwebtoken to verify a jwt and resolve with a user object like { userName: "John Doe", "permissions": ["readPrivateFiles", "readPublicFiles"] }
      return await evaluateToken(request, scopes);
  }

  return Promise.reject("invalid-authentication-method");
}

async function evaluateToken(request, scopes) {
  const user = await chekJwtAndGetUser(request);
  if (!scopes?.includes("optional") && !user) {
    throw new UnauthorizedError();
  } else if (!user) {
    return { userName: "public", permissions: ["readPublicFiles"] }
  }
  return user;
}

You can flip the conditions by using a requiredscope instead, if it's easier to understand.

from tsoa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.