Skip to content
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

Kill sessions for user #590

Merged
merged 11 commits into from
Dec 5, 2024
3 changes: 3 additions & 0 deletions auth/libraries/pip-auth/authlib/auth_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,6 @@ def verify_authorization_token(self, authorization_token: str) -> Tuple[int, str

def clear_all_sessions(self, access_token: str, refresh_id: str) -> None:
return self.session_handler.clear_all_sessions(access_token, refresh_id)

def clear_sessions_by_user_id(self, user_id: int) -> None:
return self.session_handler.clear_sessions_by_user_id(user_id)
9 changes: 9 additions & 0 deletions auth/libraries/pip-auth/authlib/session_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,12 @@ def clear_all_sessions(self, token: str, cookie: str) -> None:
)
if response.status_code != 200:
raise AuthenticateException("Failed to clear all sessions")

def clear_sessions_by_user_id(self, user_id: int) -> None:
response = self.http_handler.send_internal_request(
"clear-sessions-by-user-id", {"userId": user_id}
)
if response.status_code != 200:
raise AuthenticateException(
f"Failed to clear all sessions of user {user_id}."
)
7 changes: 7 additions & 0 deletions auth/src/express/controllers/private-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Body, OnPost, Res, RestController, Req } from 'rest-app';
import { AuthHandler } from '../../api/interfaces/auth-handler';
import { AuthService } from '../../api/services/auth-service';
import { AuthorizationException } from '../../core/exceptions/authorization-exception';
import { Id } from '../../core/key-transforms';
import { JwtPayload } from '../../core/ticket/base-jwt';
import { Token } from '../../core/ticket/token';
import { AuthServiceResponse } from '../../util/helper/definitions';
Expand Down Expand Up @@ -53,4 +54,10 @@ export class PrivateController {
throw new AuthorizationException('You are not authorized');
}
}

@OnPost('clear-sessions-by-user-id')
public async clearSessionsByUserId(@Body('userId') userId: Id): Promise<AuthServiceResponse> {
await this._authHandler.clearAllSessions(userId);
return createResponse();
}
}
27 changes: 27 additions & 0 deletions auth/test/clear-sessions-by-user-id.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Utils } from './utils';
import { TestContainer } from './test-container';

let container: TestContainer;

beforeAll(async () => {
container = new TestContainer();
await container.ready();
});

afterEach(async () => {
container.user.reset();
await container.redis.flushdb();
});

afterAll(async () => {
await container.end();
});

test('POST clear-sessions-by-user-id', async () => {
await container.request.login();
await container.http.post('clear-sessions-by-user-id', {
data: { userId: 1 },
internal: true
});
await container.request.sendRequestAndValidateForbiddenRequest(container.request.authenticate());
});
Loading