Skip to content

Commit 07fb418

Browse files
k-ylebhousel
authored andcommitted
fix popup auth broken due to new COOP header
1 parent ad63c40 commit 07fb418

File tree

9 files changed

+38
-37
lines changed

9 files changed

+38
-37
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ _Breaking changes, which may affect downstream projects, are marked with a_ ⚠
1414
1515
[#xxx]: https://github.com/osmlab/osm-auth/issues/xxx
1616
-->
17+
## 3.0.0
18+
##### 2025-Jul-08
19+
* Fix authentication broken when using the `popup` method due to [security changes on 8 July 2025](https://github.com/openstreetmap/openstreetmap-website/commit/2ff4d6) ([#138], thanks [@k-yle])
20+
21+
[#138]: https://github.com/osmlab/osm-auth/issues/138
1722

1823
## 2.6.0
1924
##### 2025-Jan-17

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
Easy authentication with [OpenStreetMap](http://www.openstreetmap.org/) over [OAuth 2.0](https://oauth.net/2/).<br/>
77
See also: https://wiki.openstreetmap.org/wiki/OAuth
88

9+
> [!IMPORTANT]
10+
> Due to [security changes on 8 July 2025](https://github.com/openstreetmap/openstreetmap-website/commit/2ff4d6), authentication using the `popup` mode will not work until you:
11+
>
12+
> 1. update this library to v3.0.0
13+
> 2. AND update the code snippet in your `land.html` file to the latest version (see [this example](https://github.com/osmlab/osm-auth/tree/master/land.html))
14+
15+
916
Note that openstreetmap.org currently only supports OAuth2.0. [OAuth1.0 is turned off](https://github.com/openstreetmap/operations/issues/867). If you want the older version of this library that supports **OAuth 1.0a** (e.g. for a sister project that uses an older OSM-stack), use [the v1 branch](https://github.com/osmlab/osm-auth/tree/v1) and pin your software to older [release versions <2](https://github.com/osmlab/osm-auth/releases). Going forward, the v1 branch will receive limited attention.
1017

1118

dist/osm-auth.cjs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ __export(osm_auth_exports, {
2222
module.exports = __toCommonJS(osm_auth_exports);
2323
function osmAuth(o) {
2424
var oauth = {};
25+
var CHANNEL_ID = "osm-api-auth-complete";
2526
var _store = null;
2627
try {
2728
_store = window.localStorage;
@@ -150,20 +151,12 @@ function osmAuth(o) {
150151
window.location = url;
151152
}
152153
} else {
153-
var popupClosedWatcher = setInterval(function() {
154-
if (popup.closed) {
155-
var error2 = new Error("Popup was closed prematurely");
156-
error2.status = "popup-closed";
157-
callback(error2);
158-
window.clearInterval(popupClosedWatcher);
159-
delete window.authComplete;
160-
}
161-
}, 1e3);
162154
oauth.popupWindow = popup;
163155
popup.location = url;
164156
}
165-
window.authComplete = function(url2) {
166-
clearTimeout(popupClosedWatcher);
157+
var bc = new BroadcastChannel(CHANNEL_ID);
158+
bc.addEventListener("message", (event) => {
159+
var url2 = event.data;
167160
var params2 = utilStringQs(url2.split("?")[1]);
168161
if (params2.state !== state) {
169162
var error2 = new Error("Invalid state");
@@ -172,8 +165,8 @@ function osmAuth(o) {
172165
return;
173166
}
174167
_getAccessToken(params2.code, pkce.code_verifier, accessTokenDone);
175-
delete window.authComplete;
176-
};
168+
bc.close();
169+
});
177170
function accessTokenDone(err, xhr) {
178171
o.done();
179172
if (err) {

dist/osm-auth.cjs.map

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

dist/osm-auth.iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/osm-auth.iife.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

land.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
<html><head></head>
33
<body>
44
<script>
5-
opener.authComplete(window.location.href);
6-
window.close();
5+
if (new URLSearchParams(location.search).has("code")) {
6+
new BroadcastChannel("osm-api-auth-complete").postMessage(location.href);
7+
window.close();
8+
}
79
</script>
810
</body>
9-
</html>
11+
</html>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "osm-auth",
3-
"version": "2.6.0",
3+
"version": "3.0.0",
44
"license": "ISC",
55
"repository": "github:osmlab/osm-auth",
66
"description": "A usable example of JavaScript OAuth 2.0 with OpenStreetMap",

src/osm-auth.mjs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
export function osmAuth(o) {
2222
var oauth = {};
2323

24+
var CHANNEL_ID = 'osm-api-auth-complete';
25+
2426
// Mock localStorage if needed.
2527
// Note that accessing localStorage may throw a `SecurityError`, so wrap in a try/catch.
2628
var _store = null;
@@ -236,23 +238,15 @@ export function osmAuth(o) {
236238
window.location = url;
237239
}
238240
} else {
239-
var popupClosedWatcher = setInterval(function() {
240-
if (popup.closed) {
241-
var error = new Error('Popup was closed prematurely');
242-
error.status = 'popup-closed';
243-
callback(error);
244-
window.clearInterval(popupClosedWatcher);
245-
delete window.authComplete;
246-
}
247-
}, 1000);
248241
oauth.popupWindow = popup;
249242
popup.location = url;
250243
}
251244

252245
// Called by a function in the redirect URL page, in the popup window. The
253246
// window closes itself.
254-
window.authComplete = function (url) {
255-
clearTimeout(popupClosedWatcher);
247+
var bc = new BroadcastChannel(CHANNEL_ID);
248+
bc.addEventListener('message', (event) => {
249+
var url = event.data;
256250
var params = utilStringQs(url.split('?')[1]);
257251
if (params.state !== state) {
258252
var error = new Error('Invalid state');
@@ -261,8 +255,8 @@ export function osmAuth(o) {
261255
return;
262256
}
263257
_getAccessToken(params.code, pkce.code_verifier, accessTokenDone);
264-
delete window.authComplete;
265-
};
258+
bc.close();
259+
});
266260

267261
function accessTokenDone(err, xhr) {
268262
o.done();

0 commit comments

Comments
 (0)