-
Notifications
You must be signed in to change notification settings - Fork 0
feat(serveur): afficher les pochettes du catalogue distant #18
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e3fd3c1
feat(serveur): afficher les pochettes du catalogue distant
InstaZDLL 50c9b5b
fix(serveur): clé de pochette par hachage, et origine complète pour l…
InstaZDLL eed85f4
fix(serveur): encoder le hachage de pochette, et couvrir sa propagation
InstaZDLL 7c36193
test(serveur): documenter pourquoi un artiste peut porter une pochette
InstaZDLL 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package app.waveflow.data.remote | ||
|
|
||
| import android.net.Uri | ||
| import androidx.core.net.toUri | ||
|
|
||
| /** | ||
| * Construit les adresses de pochettes d'un serveur. | ||
| * | ||
| * L'adresse est bâtie sur le **hachage** de la pochette, et non sur | ||
| * l'identifiant de l'entité qui la porte — `/api/v2/artwork/` accepte les deux. | ||
| * Le hachage désigne le contenu, ce qui donne deux propriétés que l'identifiant | ||
| * n'a pas : | ||
| * | ||
| * - remplacer une jaquette change le hachage, donc l'adresse, donc la clé de | ||
| * cache. Sur l'identifiant, l'ancienne image resterait affichée aussi | ||
| * longtemps que le cache la garde — le serveur annonce `max-age=86400` ; | ||
| * - un album et ses pistes portent le même hachage. Une seule entrée de cache | ||
| * et un seul téléchargement, là où l'identifiant en aurait produit autant que | ||
| * de lignes affichées. | ||
| * | ||
| * Une entité sans hachage n'a pas de pochette : aucune adresse n'est produite. | ||
| * En construire une coûterait un aller-retour par ligne pour un 404 à chaque | ||
| * fois, à chaque défilement. | ||
| */ | ||
| class ArtworkUrls(private val serverUrl: String, private val http: ServerHttp) { | ||
|
|
||
| fun forHash(artworkHash: String?): Uri? { | ||
| if (artworkHash.isNullOrBlank()) return null | ||
|
|
||
| // Le hachage est un segment à part entière, et non interpolé : il vient | ||
| // d'une réponse serveur, et un `/` qui s'y glisserait désignerait un | ||
| // autre point d'API. | ||
| // | ||
| // Construite en plein rendu d'une liste : une adresse de serveur | ||
| // invalide doit coûter une vignette, pas l'écran entier. | ||
| return runCatching { | ||
| http.absoluteUrl(serverUrl, "/$PATH", pathSegment = artworkHash).toUri() | ||
| }.getOrNull() | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| private companion object { | ||
| const val PATH = "api/v2/artwork" | ||
| } | ||
| } | ||
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
77 changes: 77 additions & 0 deletions
77
app/src/main/java/app/waveflow/data/remote/ServerImageAuth.kt
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,77 @@ | ||
| package app.waveflow.data.remote | ||
|
|
||
| import app.waveflow.model.ServerSession | ||
| import kotlinx.coroutines.runBlocking | ||
| import okhttp3.Interceptor | ||
| import okhttp3.Response | ||
|
|
||
| /** | ||
| * Porte le jeton de session sur les requêtes d'images du serveur. | ||
| * | ||
| * `/api/v2/artwork/` exige le même Bearer que le reste de l'API native, et | ||
| * Coil ne connaît rien de la session : cet intercepteur l'ajoute pour lui. | ||
| * | ||
| * Seules les requêtes vers l'origine du serveur connecté sont signées. Une | ||
| * pochette locale — un `content://` — ne passe pas par OkHttp, mais une | ||
| * jaquette venue d'ailleurs pourrait ; lui joindre le jeton reviendrait à le | ||
| * confier à un tiers. | ||
| * | ||
| * L'appel est bloquant : les intercepteurs OkHttp le sont, et s'exécutent sur | ||
| * ses propres fils. | ||
| */ | ||
| class ServerImageAuthInterceptor( | ||
| private val sessionRepository: ServerSessionRepository, | ||
| ) : Interceptor { | ||
|
|
||
| override fun intercept(chain: Interceptor.Chain): Response { | ||
| val request = chain.request() | ||
| val session = sessionRepository.session.value as? ServerSession.Connected | ||
| ?: return chain.proceed(request) | ||
|
|
||
| if (!request.url.isSameOriginAs(session.serverUrl)) return chain.proceed(request) | ||
|
|
||
| val token = runCatching { runBlocking { sessionRepository.validAccessToken() } } | ||
| .getOrNull() | ||
| ?: return chain.proceed(request) | ||
|
|
||
| val signed = chain.proceed(request.withBearer(token)) | ||
| if (signed.code != HTTP_UNAUTHORIZED) return signed | ||
|
|
||
| // Jeton révoqué ailleurs : l'horloge locale le croyait bon. Comme pour | ||
| // le catalogue, on le périme et on rejoue une fois — un second refus | ||
| // veut dire que la session est fermée, et la pochette manquera. | ||
| signed.close() | ||
| val renewed = runCatching { | ||
| runBlocking { | ||
| sessionRepository.expireAccessToken() | ||
| sessionRepository.validAccessToken() | ||
| } | ||
| }.getOrNull() ?: return chain.proceed(request) | ||
|
|
||
| return chain.proceed(request.withBearer(renewed)) | ||
| } | ||
|
|
||
| private fun okhttp3.Request.withBearer(token: String) = | ||
| newBuilder().header("Authorization", "Bearer $token").build() | ||
|
|
||
| /** | ||
| * Compare l'origine — schéma, hôte et port — à celle du serveur connecté. | ||
| * | ||
| * Le schéma compte autant que le reste : un serveur joint en HTTPS et une | ||
| * adresse en `http://` vers le même hôte et le même port enverraient le | ||
| * jeton en clair. Les ports par défaut suffisent à les distinguer quand | ||
| * ils sont implicites, pas quand le port est explicite — ce qui est le cas | ||
| * courant d'un serveur auto-hébergé. | ||
| * | ||
| * L'adresse saisie par l'utilisateur passe par la même normalisation que | ||
| * les appels d'API : sans schéma, elle est jointe en HTTPS. | ||
| */ | ||
| private fun okhttp3.HttpUrl.isSameOriginAs(serverUrl: String): Boolean { | ||
| val server = runCatching { ServerHttp.parseBase(serverUrl) }.getOrNull() ?: return false | ||
| return scheme == server.scheme && host == server.host && port == server.port | ||
| } | ||
|
|
||
| private companion object { | ||
| const val HTTP_UNAUTHORIZED = 401 | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.