Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support authentication tokens (through TransformRequest) #2299

Open
bchretien opened this issue Feb 27, 2025 · 2 comments
Open

Support authentication tokens (through TransformRequest) #2299

bchretien opened this issue Feb 27, 2025 · 2 comments

Comments

@bchretien
Copy link

New Feature

Some layers may require authentication tokens to be properly displayed in Mapbox. With mapbox-gl-js, we can use TransformRequest to do that easily (see https://docs.mapbox.com/mapbox-gl-js/api/properties/#requestparameters). Looking at the documentation, this does not seem possible in mapbox-maps-ios.

Why

Without this, we cannot display layers that require some form of authentication (e.g. bearer tokens passed in headers).

@DannyElinext
Copy link

DannyElinext commented Feb 27, 2025

I have the same problem, when I try to draw an image which requires an authentication token in header on the Mapbox through SourceType like ImageSource, VectorSource or RasterSource, then it didn't work and showed this error "[Error, maps-core]: {}[Style]: Failed to load source image_source_102: Invalid value. at offset 0". But it can work with an image that can be opened free on a browser. While Android can run normally with that requirement.

@OdNairy
Copy link
Contributor

OdNairy commented Feb 27, 2025

Hi there
The HttpServiceInterceptorInterface could help you with custom access tokens. I've made a basic sample for you:

Image

final class DebugViewController: UIViewController {

    var mapView: MapView!

    var cancellables: Set<AnyCancelable> = []

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView = MapView(frame: view.bounds)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.insertSubview(mapView, at: 0)

        addHTTPInterceptor()
    }

    func addHTTPInterceptor() {
        HttpServiceFactory.setHttpServiceInterceptorForInterceptor(SampleHTTPInterceptor())
    }
}

class SampleHTTPInterceptor: HttpServiceInterceptorInterface {
    func onRequest(for request: HttpRequest, continuation: @escaping HttpServiceInterceptorRequestContinuation) {
        request.headers["Sample-Header"] = "Sample-Value"
        continuation(HttpRequestOrResponse.fromHttpRequest(request))
    }

    func onResponse(for response: HttpResponse, continuation: @escaping HttpServiceInterceptorResponseContinuation) {
        continuation(response)
    }
}

Pay attention that setHttpServiceInterceptorForInterceptor would preserve a strong reference to your interceptor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants