-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): apikeys authentication (#49)
* feat(api): setup data layer and services * chore: renamed to sdk auth * feat(api): add isSdkAuthenticated middleware * chore(core): update sdk error codes * chore(api): added response code for missing key * chore(api): add helper methods to set response * feat(ui): reflect api changes * feat(ui): added api keys panel * feat(ui): added create and revoke apikey * chore(ui): fix navigation * chore(ui): fix table items visibility on mobile * chore(ui): minor ui changes
- Loading branch information
Showing
46 changed files
with
1,000 additions
and
271 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,27 @@ | ||
import { ApiResponseCode, ResponseModel } from "@switchfeat/core"; | ||
import { Request, Response } from "express"; | ||
|
||
export const setErrorResponse = (resp: Response, error: ApiResponseCode) => { | ||
console.log(error); | ||
resp.status(error.statusCode).json({ | ||
success: false, | ||
error: error, | ||
data: null | ||
} as ResponseModel<null>); | ||
}; | ||
|
||
export const setSuccessResponse = <T extends object | null>(resp: Response, code: ApiResponseCode, data: T, req?: Request) => { | ||
console.log(code); | ||
|
||
const response = { | ||
success: true, | ||
data | ||
} as ResponseModel<T>; | ||
|
||
if (req) { | ||
response.user = req.user; | ||
response.cookies = req.cookies; | ||
} | ||
|
||
resp.status(code.statusCode).json(response); | ||
}; |
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 |
---|---|---|
@@ -1,40 +1,68 @@ | ||
import passport from "passport"; | ||
import { Request, Response, NextFunction, Express } from "express"; | ||
import * as userService from "../../services/usersService"; | ||
import * as sdkAuthService from "../../services/sdkAuthService"; | ||
import { googleStrategy } from "./googleAuth"; | ||
import { keys } from "@switchfeat/core"; | ||
import { ApiResponseCodes, keys } from "@switchfeat/core"; | ||
|
||
|
||
export const initialise = (app: Express) => { | ||
|
||
app.use(passport.initialize()); | ||
app.use(passport.initialize()); | ||
|
||
// serialize the user.id to save in the cookie session | ||
// so the browser will remember the user when login | ||
passport.serializeUser((_req, user, done) => { | ||
done(null, user); | ||
}); | ||
// serialize the user.id to save in the cookie session | ||
// so the browser will remember the user when login | ||
passport.serializeUser((_req, user, done) => { | ||
done(null, user); | ||
}); | ||
|
||
// deserialize the cookieUserId to user in the database | ||
passport.deserializeUser(async (id: string, done) => { | ||
const currentUser = await userService.getUser({ userId: id }); | ||
done(currentUser === null ? "user not found." : null, { user: currentUser }); | ||
}); | ||
// deserialize the cookieUserId to user in the database | ||
passport.deserializeUser(async (id: string, done) => { | ||
const currentUser = await userService.getUser({ userId: id }); | ||
done(currentUser === null ? "user not found." : null, { user: currentUser }); | ||
}); | ||
|
||
if (keys.AUTH_PROVIDER === "google") { | ||
console.log(" -> Google auth active"); | ||
passport.use(googleStrategy()); | ||
} | ||
if (keys.AUTH_PROVIDER === "google") { | ||
console.log(" -> Google auth active"); | ||
passport.use(googleStrategy()); | ||
} | ||
}; | ||
|
||
export const isAuthenticated = (req: Request, res: Response, next: NextFunction) => { | ||
if (!keys.AUTH_PROVIDER || req.isAuthenticated()) { | ||
return next(); | ||
} | ||
res.redirect("/"); | ||
}; | ||
|
||
/* | ||
** - Get the apikey from the sdk request | ||
** - Lookup of the key in db | ||
** - Ensure it is not expired | ||
*/ | ||
export const isSdkAuthenticated = async (req: Request, res: Response, next: NextFunction) => { | ||
|
||
const apiKey = req.headers["sf-api-key"] as string; | ||
if (!apiKey) { | ||
res.status(401).json({ | ||
error: ApiResponseCodes.ApiKeyNotFound, | ||
}); | ||
|
||
export const isAuthenticated = (req: Request, res: Response, next: NextFunction) => { | ||
if (!keys.AUTH_PROVIDER || req.isAuthenticated()) { | ||
return next(); | ||
} | ||
res.redirect("/"); | ||
return; | ||
} | ||
const foundInDb = await sdkAuthService.getSdkAuth({ apiKey: apiKey }); | ||
|
||
const isValid = foundInDb !== null && foundInDb.expiresOn > new Date(); | ||
|
||
if (!keys.AUTH_PROVIDER && isValid) { | ||
return next(); | ||
} | ||
|
||
res.status(401).json({ | ||
error: ApiResponseCodes.ApiKeyNotValid | ||
}); | ||
|
||
return; | ||
}; | ||
|
||
|
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
Oops, something went wrong.