-
-
Notifications
You must be signed in to change notification settings - Fork 60
Add header authentication for SSO #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MaxLeiter
wants to merge
5
commits into
main
Choose a base branch
from
headerAuth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
13040ab
server: begin implementing header auth
MaxLeiter 05cc23a
remove file accidently included in rebase
MaxLeiter f74f7b1
code review: don't create auth token if using header auth
MaxLeiter 743ca20
add whitelisting IPs
MaxLeiter a30425a
/verify-token --> /verify-signed-in
MaxLeiter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,48 @@ | ||
import jwt, { UserJwtRequest } from "@lib/middleware/is-signed-in" | ||
import { NextFunction, Response } from "express" | ||
|
||
describe("jwt is-signed-in middlware", () => { | ||
let mockRequest: Partial<UserJwtRequest> | ||
let mockResponse: Partial<Response> | ||
let nextFunction: NextFunction = jest.fn() | ||
|
||
beforeEach(() => { | ||
mockRequest = {} | ||
mockResponse = { | ||
sendStatus: jest.fn().mockReturnThis() | ||
} | ||
}) | ||
|
||
it("should return 401 if no authorization header", () => { | ||
const res = mockResponse as Response | ||
jwt(mockRequest as UserJwtRequest, res, nextFunction) | ||
expect(res.sendStatus).toHaveBeenCalledWith(401) | ||
}) | ||
|
||
it("should return 401 if no token is supplied", () => { | ||
const req = mockRequest as UserJwtRequest | ||
req.headers = { | ||
authorization: "Bearer" | ||
} | ||
jwt(req, mockResponse as Response, nextFunction) | ||
expect(mockResponse.sendStatus).toBeCalledWith(401) | ||
}) | ||
|
||
// it("should return 401 if token is deleted", async () => { | ||
// try { | ||
// const tokenString = "123" | ||
|
||
// const req = mockRequest as UserJwtRequest | ||
// req.headers = { | ||
// authorization: `Bearer ${tokenString}` | ||
// } | ||
// jwt(req, mockResponse as Response, nextFunction) | ||
// expect(mockResponse.sendStatus).toBeCalledWith(401) | ||
// expect(mockResponse.json).toBeCalledWith({ | ||
// message: "Token is no longer valid" | ||
// }) | ||
// } catch (e) { | ||
// console.log(e) | ||
// } | ||
// }) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,91 @@ | ||
import { AuthToken } from "@lib/models/AuthToken" | ||
import { NextFunction, Request, Response } from "express" | ||
import * as jwt from "jsonwebtoken" | ||
import config from "../config" | ||
import { User as UserModel } from "../models/User" | ||
|
||
export interface User { | ||
id: string | ||
} | ||
|
||
export interface UserJwtRequest extends Request { | ||
user?: User | ||
} | ||
|
||
export default async function isSignedIn( | ||
req: UserJwtRequest, | ||
res: Response, | ||
next: NextFunction | ||
) { | ||
const authHeader = req.headers ? req.headers["authorization"] : undefined | ||
const token = authHeader && authHeader.split(" ")[1] | ||
|
||
if (config.header_auth && config.header_auth_name) { | ||
if (!config.header_auth_whitelisted_ips?.includes(req.ip)) { | ||
console.warn(`IP ${req.ip} is not whitelisted and tried to authenticate with header auth.`) | ||
return res.sendStatus(401) | ||
} | ||
|
||
// with header auth, we assume the user is authenticated, | ||
// but their user may not be created in the database yet. | ||
|
||
let user = await UserModel.findByPk(req.user?.id) | ||
if (!user) { | ||
const username = req.header[config.header_auth_name] | ||
const role = config.header_auth_role ? req.header[config.header_auth_role] || "user" : "user" | ||
user = new UserModel({ | ||
username, | ||
role | ||
}) | ||
await user.save() | ||
console.log(`Created user ${username} with role ${role} via header auth.`) | ||
} | ||
|
||
req.user = user | ||
next() | ||
} else { | ||
if (token == null) return res.sendStatus(401) | ||
|
||
const authToken = await AuthToken.findOne({ where: { token: token } }) | ||
if (authToken == null) { | ||
return res.sendStatus(401) | ||
} | ||
|
||
if (authToken.deletedAt) { | ||
return res.sendStatus(401).json({ | ||
message: "Token is no longer valid" | ||
}) | ||
} | ||
|
||
jwt.verify(token, config.jwt_secret, async (err: any, user: any) => { | ||
if (err) { | ||
if (config.header_auth) { | ||
// if the token has expired or is invalid, we need to delete it and generate a new one | ||
authToken.destroy() | ||
const token = jwt.sign({ id: user.id }, config.jwt_secret, { | ||
expiresIn: "2d" | ||
}) | ||
const newToken = new AuthToken({ | ||
userId: user.id, | ||
token: token | ||
}) | ||
await newToken.save() | ||
} else { | ||
return res.sendStatus(403) | ||
} | ||
} | ||
|
||
const userObj = await UserModel.findByPk(user.id, { | ||
attributes: { | ||
exclude: ["password"] | ||
} | ||
}) | ||
if (!userObj) { | ||
return res.sendStatus(403) | ||
} | ||
req.user = user | ||
|
||
next() | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this should meet my needs.