Daniele B
07/12/2023, 9:54 AMFailed to load resource: the server responded with a status of 401 ()
Access to fetch at '<https://mydomain.com/endpoint>' from origin '<http://localhost:8080>' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
This is the Ktor code:
install(Auth) {
basic {
credentials {
BasicAuthCredentials(username = authSettings.username, password = authSettings.password)
}
realm = "cms"
sendWithoutRequest { request ->
request.url.host == "0.0.0.0"
}
}
}
Are there issues on Ktor/JS?
The endpoint is a Golang webservice, with this code:
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With, WWW-Authenticate")
if !authorizedOK(r) {
w.Header().Set("WWW-Authenticate", `Basic realm="cms", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "application/json")
resp := myproject.GetCMSResponse(r)
json.NewEncoder(w).Encode(resp)
ephemient
07/12/2023, 10:22 AMDaniele B
07/12/2023, 10:30 AMw.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With, WWW-Authenticate, Origin")
if !authorizedOK(r) {
w.Header().Set("WWW-Authenticate", `Basic realm="cms", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "application/json")
resp := myproject.GetCMSResponse(r)
json.NewEncoder(w).Encode(resp)
Daniele B
07/12/2023, 11:01 AMURI: <https://mydomain.com/endpoint>
METHOD: POST
HEADERS:
Accept: application/json
Accept-Charset: UTF-8
Connection: Keep-Alive
Content-Length: 0
User-Agent: Ktor client
RESPONSE by server: NOT authorized
REQUEST 2:
URI: <https://mydomain.com/endpoint>
METHOD: POST
HEADERS:
Accept: application/json
Accept-Charset: UTF-8
Authorization: Basic ZGFuaAbsZTp1ZmlttWM5Mw==
Connection: Keep-Alive
Content-Length: 0
User-Agent: Ktor client
RESPONSE by server: AUTHORIZED
===
These are the requests by the WEB/JS app:
REQUEST 1:
URI: <https://mydomain.com/endpoint>
METHOD: POST
HEADERS:
Accept: application/json
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,it-IT;q=0.8,it;q=0.7,en-US;q=0.6,th;q=0.5
Content-Length: 0
Origin: <http://localhost:8080>
Referer: <http://localhost:8080/>
Sec-Ch-Ua: "Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "macOS"
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
RESPONSE by server: NOT authorized
REQUEST 2:
URI: <https://mydomain.com/endpoint>
METHOD: OPTIONS
HEADERS:
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,it-IT;q=0.8,it;q=0.7,en-US;q=0.6,th;q=0.5
Access-Control-Request-Headers: authorization
Access-Control-Request-Method: POST
Origin: <http://localhost:8080>
Referer: <http://localhost:8080/>
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
RESPONSE by server: NOT authorizedDaniele B
07/12/2023, 11:02 AMDaniele B
07/12/2023, 11:03 AMephemient
07/12/2023, 3:53 PMephemient
07/12/2023, 3:53 PMDaniele B
07/13/2023, 3:22 AMw.Header().Set("Access-Control-Allow-Origin", "*")
if r.Method == http.MethodOptions {
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Origin")
return
}
if !authorizedOK(r) {
w.Header().Set("WWW-Authenticate", `Basic realm="cms", charset="UTF-8"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "application/json")
resp := myproject.GetCMSResponse(r)
json.NewEncoder(w).Encode(resp)
Daniele B
07/13/2023, 3:50 AMAleksei Tirman [JB]
07/13/2023, 8:56 AMDaniele B
07/13/2023, 12:04 PMandylamax
07/13/2023, 3:35 PMDaniele B
07/14/2023, 7:23 AMDaniele B
07/14/2023, 7:26 AM401
(Unauthorized) response with the WWW-Authenticate
header, you need to call the sendWithoutRequest
function returning boolean and check the request parameters"Aleksei Tirman [JB]
07/14/2023, 7:37 AMHowever the fact that even on Desktop, the first client request doesn't include the "Authorization" header, makes me think that the problem isn't in the backend.Can you please file an issue with a code snippet for reproduction attached?
Daniele B
07/14/2023, 11:58 AMDaniele B
07/14/2023, 1:18 PMsendWithoutRequest { request -> request.url.host == "0.0.0.0" }
didn't include localhost:8080
with this it works!
sendWithoutRequest { request -> true }
Daniele B
07/16/2023, 6:19 AM