Push.Subscription: convert base64 to base64 urlsafe

This commit is contained in:
href 2018-12-08 17:32:58 +01:00
parent 324933a0ac
commit 7facbb2b8d
No known key found for this signature in database
GPG Key ID: EE8296C1A152C325

View File

@ -37,8 +37,8 @@ def create(
user_id: user.id, user_id: user.id,
token_id: token.id, token_id: token.id,
endpoint: endpoint, endpoint: endpoint,
key_auth: key_auth, key_auth: ensure_base64_urlsafe(key_auth),
key_p256dh: key_p256dh, key_p256dh: ensure_base64_urlsafe(key_p256dh),
data: alerts(params) data: alerts(params)
}) })
end end
@ -63,4 +63,14 @@ def delete_if_exists(user, token) do
sub -> Repo.delete(sub) sub -> Repo.delete(sub)
end end
end end
# Some webpush clients (e.g. iOS Toot!) use an non urlsafe base64 as an encoding for the key.
# However, the web push rfs specify to use base64 urlsafe, and the `web_push_encryption` library we use
# requires the key to be properly encoded. So we just convert base64 to urlsafe base64.
defp ensure_base64_urlsafe(string) do
string
|> String.replace("+", "-")
|> String.replace("/", "_")
|> String.replace("=", "")
end
end end