Skip to content

Commit

Permalink
Allow setting auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Jan 27, 2020
1 parent d51cea5 commit ddd675c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc and others.
* Copyright (c) 2019, 2020 Red Hat Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -58,11 +58,13 @@ public static Optional<Registration> fromEnv(final String tenantId) {
.map(Version::valueOf)
.orElse(Version.V1);

final String token = System.getenv("DEVICE_REGISTRY_TOKEN");

System.out.format("Device Registry - Version: %s, URL: %s%n", version, devRegUrl);

switch (version) {
case V1:
return of(new RegistrationV1(tenantId, parse(devRegUrl)));
return of(new RegistrationV1(tenantId, token, parse(devRegUrl)));
default:
return of(new RegistrationLegacy(tenantId, parse(devRegUrl)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2019 Red Hat Inc and others.
* Copyright (c) 2017, 2020 Red Hat Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -20,6 +20,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.net.HttpHeaders;

import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.RequestBody;
Expand All @@ -32,9 +34,17 @@ public class RegistrationV1 extends AbstractRegistration {
private final HttpUrl registrationUrl;
private final HttpUrl credentialsUrl;

public RegistrationV1(final String tenantId, final HttpUrl deviceRegistryUrl) {
private final String authzString;

public RegistrationV1(final String tenantId, String token, final HttpUrl deviceRegistryUrl) {
super(tenantId);

if (token != null && !token.isBlank()) {
this.authzString = String.format("Bearer %s", token);
} else {
this.authzString = null;
}

this.registrationUrl = deviceRegistryUrl
.newBuilder()
.addPathSegment("devices")
Expand Down Expand Up @@ -63,6 +73,7 @@ public void device(final String deviceId, final String username,
.addPathSegment(this.tenantId)
.addPathSegment(deviceId)
.build())
.addHeader(HttpHeaders.AUTHORIZATION, this.authzString)
.get()
.build()).execute()) {

Expand All @@ -83,6 +94,7 @@ public void device(final String deviceId, final String username,
.addPathSegment(this.tenantId)
.addPathSegment(deviceId)
.build())
.addHeader(HttpHeaders.AUTHORIZATION, this.authzString)
.post(RequestBody.create(MT_JSON, "{}" /* empty object */))
.build()).execute()) {

Expand Down Expand Up @@ -110,7 +122,8 @@ public void device(final String deviceId, final String username,
.addPathSegment(this.tenantId)
.addPathSegment(deviceId)
.build())
.put(RequestBody.create(MT_JSON, encode(new CommonCredential[] { pc })))
.addHeader(HttpHeaders.AUTHORIZATION, this.authzString)
.put(RequestBody.create(MT_JSON, encode(new CommonCredential[] {pc})))
.build())
.execute()) {

Expand Down

0 comments on commit ddd675c

Please sign in to comment.