Skip to content

Commit 6b2f3e8

Browse files
committed
Always try to remove Web Push subscription instead of checking if the
revoke is for the mobile scope. This allow to simplify code and move code from app to modules and simplify it in exchange of more calls to the db.
1 parent c95d9c3 commit 6b2f3e8

3 files changed

Lines changed: 26 additions & 35 deletions

File tree

app/controllers/OAuth.scala

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import lila.app.*
99
import lila.common.HTTPRequest
1010
import lila.common.Json.given
1111
import lila.core.net.Bearer
12-
import lila.oauth.{ AccessToken, AccessTokenRequest, AuthorizationRequest, OAuthScopes }
12+
import lila.oauth.{ AccessTokenRequest, AuthorizationRequest, OAuthScopes }
1313

1414
import Api.ApiResult
1515

@@ -100,23 +100,14 @@ final class OAuth(env: Env, apiC: => Api) extends LilaController(env):
100100

101101
def tokenRevoke = Scoped() { ctx ?=> _ ?=>
102102
HTTPRequest.bearer(ctx.req).so { token =>
103-
for
104-
_ <- env.oAuth.tokenApi.revoke(token)
105-
_ <- ctx.isMobileOauth.soFu:
106-
env.push.webSubscriptionApi.unsubscribeBySession(AccessToken.Id.from(token).value)
107-
yield NoContent
103+
env.oAuth.tokenApi.revoke(token).inject(NoContent)
108104
}
109105
}
110106

111107
def revokeClient = AuthBody { ctx ?=> _ ?=>
112108
bindForm(lila.oauth.AccessTokenRequest.revokeClientForm)(
113109
_ => BadRequest,
114-
origin =>
115-
for
116-
tokens <- env.oAuth.tokenApi.revokeByClientOrigin(origin)
117-
_ <- (origin == "org.lichess.mobile://").soFu:
118-
tokens.traverse(token => env.push.webSubscriptionApi.unsubscribeBySession(token.value))
119-
yield NoContent
110+
origin => env.oAuth.tokenApi.revokeByClientOrigin(origin).inject(NoContent)
120111
)
121112
}
122113

modules/oauth/src/main/AccessTokenApi.scala

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -176,29 +176,26 @@ final class AccessTokenApi(
176176
.runWith(Sink.ignore)
177177
.void
178178

179-
def revokeByClientOrigin(clientOrigin: String)(using me: MyId): Fu[List[AccessToken.Id]] =
180-
for
181-
tokens <- coll
182-
.find(
183-
$doc(
184-
F.userId -> me,
185-
F.clientOrigin -> clientOrigin
186-
),
187-
$doc(F.id -> 1).some
188-
)
189-
.sort($sort.desc(F.usedAt))
190-
.cursor[Bdoc]()
191-
.list(100)
192-
.flatMap: invalidate =>
193-
coll.delete
194-
.one:
195-
$doc(
196-
F.userId -> me,
197-
F.clientOrigin -> clientOrigin
198-
)
199-
.map(_ => invalidate.flatMap(_.getAsOpt[AccessToken.Id](F.id)))
200-
_ = tokens.foreach(onRevoke(_))
201-
yield tokens
179+
def revokeByClientOrigin(clientOrigin: String)(using me: MyId): Funit =
180+
coll
181+
.find(
182+
$doc(
183+
F.userId -> me,
184+
F.clientOrigin -> clientOrigin
185+
),
186+
$doc(F.id -> 1).some
187+
)
188+
.sort($sort.desc(F.usedAt))
189+
.cursor[Bdoc]()
190+
.list(100)
191+
.flatMap: invalidate =>
192+
coll.delete
193+
.one:
194+
$doc(
195+
F.userId -> me,
196+
F.clientOrigin -> clientOrigin
197+
)
198+
.map(_ => invalidate.flatMap(_.getAsOpt[AccessToken.Id](F.id)).foreach(onRevoke))
202199

203200
def revoke(bearer: Bearer) =
204201
val id = AccessToken.Id.from(bearer)

modules/push/src/main/Env.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ final class Env(
5050
f.logFailure(logger)
5151
()
5252

53+
Bus.sub[lila.core.misc.oauth.TokenRevoke]: token =>
54+
webSubscriptionApi.unsubscribeBySession(token.id)
55+
5356
Bus.sub[lila.core.game.FinishGame]: f =>
5457
logUnit { pushApi.finish(f.game) }
5558

0 commit comments

Comments
 (0)