Search code examples
mavengradleartifactoryapi-key

How to access a maven repo on artifactory in gradle using API key?


We have a JFrog artifactory that contains a maven repo (and other elements).

In my gradle setup, I defined this:

repositories {
    maven {
        url = uri("https://eu.artifactory....../whatever/")    
        credentials {
            username = "my-full-user-id"
            password = "my-real-password"
        }
    }
}

Works fine. It also works when I change the password entry to use my JFrog API KEY.

But: using the api key only works when username contains my real user id.

Whereas: when making https requests "directly" to JFrog (for example using a python script), it is not required to provide a user name: simply having the API KEY in the request header is enough.

Thing is: in our setup, we have the API key at hand, but not the user id. So I had hoped to be able to define a maven dependency in gradle that works without user names.

But when

  • I leave username blank in my gradle setup, gradle complains about it being null.
  • I try anything else but my real user id, I get a 401 Unauthorized response

Question: is it possible to use "maven style" repository definition in gradle that works with API KEYS and that doesn't need the corresponding user ids?


Solution

  • Artifactory supports header based authentication where you provide the API key in the header X-JFrog-Art-Api (see Artifactory REST API). I do not have an Artifactory instance at hand to test, but Gradle's HTTP header authentication should do the trick:

    repositories {
        maven {
            url = uri("http://repo.mycompany.com/maven2")
            credentials(HttpHeaderCredentials::class) {
                name = "Private-Token"
                value = "TOKEN"
            }
            authentication {
                create<HttpHeaderAuthentication>("header")
            }
        }
    }