Skip to content

Commit

Permalink
Implementing OAuth2 and pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
potitoaghilar committed May 9, 2021
1 parent c8bb83f commit c643f37
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ services:
- URLS_LOGOUT=http://127.0.0.1:9020/logout
- DSN=postgres://hydra:secret@postgres:5432/hydra?sslmode=disable&max_conns=20&max_idle_conns=4
- SECRETS_SYSTEM=${SECRETS_SYSTEM}
- TTL_ACCESS_TOKEN=30s # 1 hour before expiration
- TTL_ACCESS_TOKEN=1h # 1 hour before expiration
- TTL_REFRESH_TOKEN=720h # 30 days before expiration
- OIDC_SUBJECT_IDENTIFIERS_SUPPORTED_TYPES=public,pairwise
- OIDC_SUBJECT_IDENTIFIERS_PAIRWISE_SALT=youReallyNeedToChangeThis
Expand Down
6 changes: 3 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ model Token {
id Int @id @default(autoincrement())
userId String
user User @relation(fields: [userId], references: [id])
accessToken String @unique
refreshToken String @unique
accessToken String
refreshToken String
accessTokenExpirationDate DateTime
refreshTokenExpirationDate DateTime?
rawData Json
@@index([accessToken, refreshToken])
@@unique([userId, accessToken, refreshToken])
}

model User {
Expand Down
11 changes: 6 additions & 5 deletions src/auth/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ export default async function registerBearerTokenStrategy(server: Hapi.Server) {

try {

// Keep track of previous access and refresh tokens
const prevAccessToken: string = oauthToken.accessToken
const prevRefreshToken: string = oauthToken.refreshToken

// Try to refresh access code
const refreshedOauthToken = await oauthToken.refresh()

// Update new tokens to database
await TokenRepository.updateTokenUserBind(userId, oauthToken, refreshedOauthToken)
await TokenRepository.updateTokenUserBind(userId, prevAccessToken, prevRefreshToken, refreshedOauthToken)

// Exit if JWT secret is not set
if (!process.env.JWT_SECRET) {
Expand All @@ -77,9 +81,6 @@ export default async function registerBearerTokenStrategy(server: Hapi.Server) {
accessToken: refreshedOauthToken.accessToken
}, process.env.JWT_SECRET)

// TODO remove
console.log(jwtToken)

// Notify client to change access token for next requests
return { isValid: true, credentials: { token: jwtToken } }

Expand All @@ -106,6 +107,6 @@ export default async function registerBearerTokenStrategy(server: Hapi.Server) {
response.header('Authorization', request.auth.credentials.token)
}
return h.continue;
}) as Method);
}) as Method)

}
6 changes: 3 additions & 3 deletions src/repositories/core/oauth2/token-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ export default class TokenRepository {
)
}

public static async updateTokenUserBind(userId: string, previousOAuthToken: ClientOAuth2.Token, newOAuthToken: ClientOAuth2.Token): Promise<void> {
PrismaProvider.getClient().token.update({
where: { accessToken: previousOAuthToken.accessToken, refreshToken: previousOAuthToken.refreshToken },
public static async updateTokenUserBind(userId: string, previousAccessToken: string, previousRefreshToken: string, newOAuthToken: ClientOAuth2.Token): Promise<void> {
return PrismaProvider.getClient().token.update({
where: { userId_accessToken_refreshToken: { userId, accessToken: previousAccessToken, refreshToken: previousRefreshToken } },
data: {
userId,
accessToken: newOAuthToken.accessToken,
Expand Down
1 change: 0 additions & 1 deletion src/seed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import PrismaProvider from "./repositories/core/prisma/prisma-provider";


async function createFirstUser() {
return PrismaProvider.getClient().user.create({
data: {
Expand Down

0 comments on commit c643f37

Please sign in to comment.