-
Notifications
You must be signed in to change notification settings - Fork 0
fix(llc): fix userId for guest #128
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
renefloor
wants to merge
2
commits into
main
Choose a base branch
from
renefloor/flu-373-guest-and-anonymous-login
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
2 commits
Select commit
Hold shift + click to select a range
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
95 changes: 95 additions & 0 deletions
95
packages/stream_core/test/api/interceptors/auth_interceptor_test.dart
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,95 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:stream_core/stream_core.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| // A minimal HttpClientAdapter that captures the outgoing RequestOptions and | ||
| // always responds with an empty successful response. | ||
| class _CapturingHttpClientAdapter implements HttpClientAdapter { | ||
| RequestOptions? lastRequest; | ||
|
|
||
| @override | ||
| Future<ResponseBody> fetch( | ||
| RequestOptions options, | ||
| Stream<Uint8List>? requestStream, | ||
| Future<void>? cancelFuture, | ||
| ) async { | ||
| lastRequest = options; | ||
| return ResponseBody.fromString( | ||
| '{}', | ||
| 200, | ||
| headers: { | ||
| Headers.contentTypeHeader: [Headers.jsonContentType], | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| void close({bool force = false}) {} | ||
| } | ||
|
|
||
| UserToken _generateTestUserToken(String userId) { | ||
| String b64UrlNoPad(Object jsonObj) { | ||
| final bytes = utf8.encode(jsonEncode(jsonObj)); | ||
| return base64Url.encode(bytes).replaceAll('=', ''); | ||
| } | ||
|
|
||
| final header = {'alg': 'none', 'typ': 'JWT'}; | ||
| final payload = {'user_id': userId}; | ||
|
|
||
| final jwt = '${b64UrlNoPad(header)}.${b64UrlNoPad(payload)}.'; | ||
| return UserToken(jwt); | ||
| } | ||
|
|
||
| void main() { | ||
| group('AuthInterceptor', () { | ||
| test( | ||
| "uses the resolved token's own user id for the user_id query " | ||
| 'parameter, not the id the TokenManager was constructed with', | ||
| () async { | ||
| // Simulates a token provider (e.g. a guest exchange) that resolves | ||
| // to a different id than the one originally requested. | ||
| final tokenManager = TokenManager( | ||
| userId: 'requested-id', | ||
| tokenProvider: TokenProvider.dynamic( | ||
| (_) async => _generateTestUserToken('server-assigned-id'), | ||
| ), | ||
| ); | ||
|
|
||
| final dio = Dio(BaseOptions(baseUrl: 'https://example.com')); | ||
| final adapter = _CapturingHttpClientAdapter(); | ||
| dio.httpClientAdapter = adapter; | ||
| dio.interceptors.add(AuthInterceptor(dio, tokenManager)); | ||
|
|
||
| await dio.get<void>('/test'); | ||
|
|
||
| expect( | ||
| adapter.lastRequest?.queryParameters['user_id'], | ||
| 'server-assigned-id', | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| test( | ||
| 'matches the TokenManager userId when the token resolves to the ' | ||
| 'same id (regular/anonymous users)', | ||
| () async { | ||
| final tokenManager = TokenManager( | ||
| userId: 'user-123', | ||
| tokenProvider: TokenProvider.static( | ||
| _generateTestUserToken('user-123'), | ||
| ), | ||
| ); | ||
|
|
||
| final dio = Dio(BaseOptions(baseUrl: 'https://example.com')); | ||
| final adapter = _CapturingHttpClientAdapter(); | ||
| dio.httpClientAdapter = adapter; | ||
| dio.interceptors.add(AuthInterceptor(dio, tokenManager)); | ||
|
|
||
| await dio.get<void>('/test'); | ||
|
|
||
| expect(adapter.lastRequest?.queryParameters['user_id'], 'user-123'); | ||
| }, | ||
| ); | ||
| }); | ||
| } |
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.
In my opinion the token should always match the id set in the token manager. if its different we should expire the current token and get a new token for the user id set in the manager.
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.
We already throw an error if the loaded token mismatches the user id.
https://github.com/GetStream/stream-core-flutter/blob/main/packages/stream_core/lib/src/user/token_provider.dart#L111-L113