Fix: fix #1684; add 'openid' to auth-tokens (will be needed in the future for geovisio integration)

This commit is contained in:
Pieter Vander Vennet 2023-10-24 00:35:42 +02:00
parent 8b018b7ed2
commit 0a82dd0edf
2 changed files with 40 additions and 20 deletions

View file

@ -6,6 +6,7 @@ import { Utils } from "../../Utils"
import { LocalStorageSource } from "../Web/LocalStorageSource" import { LocalStorageSource } from "../Web/LocalStorageSource"
import { AuthConfig } from "./AuthConfig" import { AuthConfig } from "./AuthConfig"
import Constants from "../../Models/Constants" import Constants from "../../Models/Constants"
import OSMAuthInstance = OSMAuth.OSMAuthInstance
export default class UserDetails { export default class UserDetails {
public loggedIn = false public loggedIn = false
@ -29,7 +30,7 @@ export default class UserDetails {
export type OsmServiceState = "online" | "readonly" | "offline" | "unknown" | "unreachable" export type OsmServiceState = "online" | "readonly" | "offline" | "unknown" | "unreachable"
export class OsmConnection { export class OsmConnection {
public auth public auth: OSMAuthInstance
public userDetails: UIEventSource<UserDetails> public userDetails: UIEventSource<UserDetails>
public isLoggedIn: Store<boolean> public isLoggedIn: Store<boolean>
public gpxServiceIsOnline: UIEventSource<OsmServiceState> = new UIEventSource<OsmServiceState>( public gpxServiceIsOnline: UIEventSource<OsmServiceState> = new UIEventSource<OsmServiceState>(
@ -118,17 +119,16 @@ export class OsmConnection {
const self = this const self = this
this.auth.bootstrapToken( this.auth.bootstrapToken(
options.oauth_token.data, options.oauth_token.data,
(x) => { (err, result) => {
console.log("Called back: ", x) console.log("Bootstrap token called back", err, result)
self.AttemptLogin() self.AttemptLogin()
}, }
this.auth
) )
options.oauth_token.setData(undefined) options.oauth_token.setData(undefined)
} }
if (this.auth.authenticated() && options.attemptLogin !== false) { if (this.auth.authenticated() && options.attemptLogin !== false) {
this.AttemptLogin() // Also updates the user badge this.AttemptLogin()
} else { } else {
console.log("Not authenticated") console.log("Not authenticated")
} }
@ -263,17 +263,33 @@ export class OsmConnection {
/** /**
* Interact with the API. * Interact with the API.
* *
* @param path: the path to query, without host and without '/api/0.6'. Example 'notes/1234/close' * @param path the path to query, without host and without '/api/0.6'. Example 'notes/1234/close'
* @param method
* @param header
* @param content
* @param allowAnonymous if set, will use the anonymous-connection if the main connection is not authenticated
*/ */
public async interact( public async interact(
path: string, path: string,
method: "GET" | "POST" | "PUT" | "DELETE", method: "GET" | "POST" | "PUT" | "DELETE",
header?: Record<string, string | number>, header?: Record<string, string | number>,
content?: string content?: string,
): Promise<any> { allowAnonymous: boolean = false
): Promise<string> {
let connection: OSMAuthInstance = this.auth
if(allowAnonymous && !this.auth.authenticated()) {
const possibleResult = await Utils.downloadAdvanced(`${this.Backend()}/api/0.6/${path}`,header, method, content)
if(possibleResult["content"]) {
return possibleResult["content"]
}
console.error(possibleResult)
throw "Could not interact with OSM:"+possibleResult["error"]
}
return new Promise((ok, error) => { return new Promise((ok, error) => {
this.auth.xhr( connection.xhr(
{ <any> {
method, method,
options: { options: {
header, header,
@ -295,9 +311,10 @@ export class OsmConnection {
public async post( public async post(
path: string, path: string,
content?: string, content?: string,
header?: Record<string, string | number> header?: Record<string, string | number>,
allowAnonymous: boolean = false
): Promise<any> { ): Promise<any> {
return await this.interact(path, "POST", header, content) return await this.interact(path, "POST", header, content, allowAnonymous)
} }
public async put( public async put(
@ -353,9 +370,10 @@ export class OsmConnection {
// Lat and lon must be strings for the API to accept it // Lat and lon must be strings for the API to accept it
const content = `lat=${lat}&lon=${lon}&text=${encodeURIComponent(text)}` const content = `lat=${lat}&lon=${lon}&text=${encodeURIComponent(text)}`
const response = await this.post("notes.json", content, { const response = await this.post("notes.json", content, {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}) }, true)
const parsed = JSON.parse(response) const parsed = JSON.parse(response)
console.log("Got result:", parsed)
const id = parsed.properties const id = parsed.properties
console.log("OPENED NOTE", id) console.log("OPENED NOTE", id)
return id return id
@ -489,13 +507,14 @@ export class OsmConnection {
this.auth = new osmAuth({ this.auth = new osmAuth({
client_id: this._oauth_config.oauth_client_id, client_id: this._oauth_config.oauth_client_id,
url: this._oauth_config.url, url: this._oauth_config.url,
scope: "read_prefs write_prefs write_api write_gpx write_notes", scope: "read_prefs write_prefs write_api write_gpx write_notes openid",
redirect_uri: Utils.runningFromConsole redirect_uri: Utils.runningFromConsole
? "https://mapcomplete.org/land.html" ? "https://mapcomplete.org/land.html"
: window.location.protocol + "//" + window.location.host + "/land.html", : window.location.protocol + "//" + window.location.host + "/land.html",
singlepage: !standalone, singlepage: !standalone,
auto: true, auto: true,
}) })
} }
private CheckForMessagesContinuously() { private CheckForMessagesContinuously() {

View file

@ -972,7 +972,9 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
*/ */
public static downloadAdvanced( public static downloadAdvanced(
url: string, url: string,
headers?: any headers?: any,
method: "POST" | "GET" | "PUT" | "UPDATE" | "DELETE" | "OPTIONS" = "GET",
content?: string
): Promise< ): Promise<
| { content: string } | { content: string }
| { redirect: string } | { redirect: string }
@ -999,14 +1001,13 @@ In the case that MapComplete is pointed to the testing grounds, the edit will be
}) })
} }
} }
xhr.open("GET", url) xhr.open(method, url)
if (headers !== undefined) { if (headers !== undefined) {
for (const key in headers) { for (const key in headers) {
xhr.setRequestHeader(key, headers[key]) xhr.setRequestHeader(key, headers[key])
} }
} }
xhr.send(content)
xhr.send()
xhr.onerror = reject xhr.onerror = reject
}) })
} }