Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #53 from TheThingsNetwork/2.1.2
Browse files Browse the repository at this point in the history
2.1.2
  • Loading branch information
johanstokking authored Apr 23, 2017
2 parents a932120 + 1247100 commit 030904c
Show file tree
Hide file tree
Showing 29 changed files with 499 additions and 49 deletions.
8 changes: 4 additions & 4 deletions account/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.thethingsnetwork</groupId>
<artifactId>app-sdk</artifactId>
<version>2.1.1</version>
<version>2.1.2</version>
</parent>
<artifactId>account</artifactId>
<packaging>jar</packaging>
Expand All @@ -24,17 +24,17 @@
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>1.2.3</version>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.5.0</version>
<version>3.6.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import okhttp3.Response;
import org.thethingsnetwork.account.async.auth.token.AsyncOAuth2Token;
import org.thethingsnetwork.account.common.AbstractApplication;
import org.thethingsnetwork.account.common.AccessKey;
import org.thethingsnetwork.account.common.ExtendedAccessKey;
import org.thethingsnetwork.account.common.ApplicationRights;
import org.thethingsnetwork.account.common.Collaborator;
import org.thethingsnetwork.account.sync.Application;
Expand Down Expand Up @@ -349,16 +349,16 @@ public Observable<AsyncApplication> removeCollaborator(Collaborator _collaborato
*
* @return the list of AccessKey of this AsyncApplication as an Observable stream.
*/
public Observable<AccessKey> getAccessKeys() {
public Observable<ExtendedAccessKey> getAccessKeys() {
/**
* GET /applications/{app_id}/access-keys
*/
return HttpRequest
.from(creds.getAccountServer() + "/applications/" + getId() + "/access-keys")
.flatMap((HttpRequest t) -> t.inject(creds))
.doOnNext((HttpRequest t) -> t.getBuilder().get())
.flatMap((HttpRequest t) -> t.doExecuteForType(AccessKey[].class))
.flatMap((AccessKey[] cs) -> Observable.from(cs));
.flatMap((HttpRequest t) -> t.doExecuteForType(ExtendedAccessKey[].class))
.flatMap((ExtendedAccessKey[] cs) -> Observable.from(cs));
}

/**
Expand All @@ -367,15 +367,15 @@ public Observable<AccessKey> getAccessKeys() {
* @param _keyname the name of the AccessKey
* @return the AccessKey as an Observable stream.
*/
public Observable<AccessKey> findOneAccessKey(String _keyname) {
public Observable<ExtendedAccessKey> findOneAccessKey(String _keyname) {
/**
* GET /applications/{app_id}/access-keys/{keyname}
*/
return HttpRequest
.from(creds.getAccountServer() + "/applications/" + getId() + "/access-keys/" + _keyname)
.flatMap((HttpRequest t) -> t.inject(creds))
.doOnNext((HttpRequest t) -> t.getBuilder().get())
.flatMap((HttpRequest t) -> t.doExecuteForType(AccessKey.class));
.flatMap((HttpRequest t) -> t.doExecuteForType(ExtendedAccessKey.class));
}

/**
Expand All @@ -384,7 +384,7 @@ public Observable<AccessKey> findOneAccessKey(String _keyname) {
* @param _key the AccessKey template
* @return the new AccessKey as an Observable stream.
*/
public Observable<AccessKey> addAccessKey(AccessKey _key) {
public Observable<ExtendedAccessKey> addAccessKey(ExtendedAccessKey _key) {
/**
* POST /applications/{app_id}/access-keys
*/
Expand All @@ -398,8 +398,8 @@ public Observable<AccessKey> addAccessKey(AccessKey _key) {
return t;
})
)
.flatMap((HttpRequest t) -> t.doExecuteForType(AccessKey.class))
.map((AccessKey c) -> c);
.flatMap((HttpRequest t) -> t.doExecuteForType(ExtendedAccessKey.class))
.map((ExtendedAccessKey c) -> c);
}

/**
Expand All @@ -408,7 +408,7 @@ public Observable<AccessKey> addAccessKey(AccessKey _key) {
* @param _key the AccessKey
* @return the updated AsyncApplication as an Observable stream.
*/
public Observable<AsyncApplication> removeAccessKey(AccessKey _key) {
public Observable<AsyncApplication> removeAccessKey(ExtendedAccessKey _key) {
/**
* DELETE /applications/{app_id}/access-keys/{keyname}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* The MIT License
*
* Copyright (c) 2017 The Things Network
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.thethingsnetwork.account.async.auth.grant;

import java.net.URI;
import org.thethingsnetwork.account.async.auth.token.AsyncAccessKey;
import org.thethingsnetwork.account.common.GrantType;
import rx.Observable;

/**
* This token provider uses application credentials (access-key) to generate a token only usable for the owning application.
* It will always use access keys for authentication.
*
* @author Romain Cambier
*/
public class AsyncApplicationAccessKey extends GrantType {

private final String key;
private final URI accountServer;

/**
* Create an instance of this token provider using fully-customized settings
*
* @param _key The application access-key
* @param _accountServer The account server to be used
*/
public AsyncApplicationAccessKey(String _key, URI _accountServer) {
if (_key == null) {
throw new IllegalArgumentException("key can not be null");
}
if (_accountServer == null) {
throw new IllegalArgumentException("accountServer can not be null");
}
key = _key;
accountServer = _accountServer;
}

/**
* Create an instance of this token provider using default account server
*
* @param _key The application access-key
*/
public AsyncApplicationAccessKey(String _key) {
this(_key, GrantType.DEFAULT_ACCOUNT_SERVER);
}

@Override
public URI getAccountServer() {
return accountServer;
}

/**
* Create a token using the settings provided in the constructor
*
* @return the AsyncAccessKey as an Observable stream
*/
public Observable<AsyncAccessKey> getToken() {
return Observable.just(new AsyncAccessKey(key, accountServer));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

/**
* This token provider uses application credentials (id + access-key) to generate a token only usable for the owning application
* It will exchange the access key for an access token using client credentials.
*
* @author Romain Cambier
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.thethingsnetwork.account.async.auth.token;

import java.net.URI;
import rx.Observable;

/**
* Async Access Key wrapper
*
* @author Romain Cambier
*/
public class AsyncAccessKey implements AsyncOAuth2Token {

private final String accessKey;
private final URI accountServer;

public AsyncAccessKey(String _accessKey, URI _accountServer) {
accessKey = _accessKey;
accountServer = _accountServer;
}

@Override
public boolean hasRefresh() {
return false;
}

@Override
public Observable<? extends AsyncOAuth2Token> refresh() {
return Observable.error(new UnsupportedOperationException("Not supported."));
}

@Override
public boolean isExpired() {
return false;
}

@Override
public String getToken() {
return "Key " + accessKey;
}

@Override
public String getRawToken() {
return accessKey;
}

@Override
public URI getAccountServer() {
return accountServer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @author Romain Cambier
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AccessKey {
public class ExtendedAccessKey {

private String name;
private String key;
Expand All @@ -41,7 +41,7 @@ public class AccessKey {
/**
* Create an empty AccessKey. Only used by jackson.
*/
public AccessKey() {
public ExtendedAccessKey() {

}

Expand All @@ -51,7 +51,7 @@ public AccessKey() {
* @param _name The key name
* @param _rights The key rights
*/
public AccessKey(String _name, List<ApplicationRights> _rights) {
public ExtendedAccessKey(String _name, List<ApplicationRights> _rights) {
name = _name;
rights = _rights;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class GrantType {

static {
try {
DEFAULT_ACCOUNT_SERVER = new URI("https://account.thethingsnetwork.org");
DEFAULT_ACCOUNT_SERVER = new URI("https://account.thethingsnetwork.org/api/v2");
} catch (URISyntaxException ex) {
throw new RuntimeException(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.List;
import org.thethingsnetwork.account.async.AsyncApplication;
import org.thethingsnetwork.account.common.AbstractApplication;
import org.thethingsnetwork.account.common.AccessKey;
import org.thethingsnetwork.account.common.ExtendedAccessKey;
import org.thethingsnetwork.account.common.ApplicationRights;
import org.thethingsnetwork.account.common.Collaborator;
import org.thethingsnetwork.account.sync.auth.token.OAuth2Token;
Expand Down Expand Up @@ -224,7 +224,7 @@ public Application removeCollaborator(Collaborator _collaborator) {
*
* @return the list of AccessKey of this Application
*/
public List<AccessKey> getAccessKeys() {
public List<ExtendedAccessKey> getAccessKeys() {
return wrapped.getAccessKeys()
.toList()
.toBlocking()
Expand All @@ -237,7 +237,7 @@ public List<AccessKey> getAccessKeys() {
* @param _keyname the name of the AccessKey
* @return the AccessKey
*/
public AccessKey findOneAccessKey(String _keyname) {
public ExtendedAccessKey findOneAccessKey(String _keyname) {
return wrapped.findOneAccessKey(_keyname)
.toBlocking()
.singleOrDefault(null);
Expand All @@ -249,7 +249,7 @@ public AccessKey findOneAccessKey(String _keyname) {
* @param _key the AccessKey template
* @return the new AccessKey
*/
public AccessKey addAccessKey(AccessKey _key) {
public ExtendedAccessKey addAccessKey(ExtendedAccessKey _key) {
return wrapped.addAccessKey(_key)
.toBlocking()
.single();
Expand All @@ -261,7 +261,7 @@ public AccessKey addAccessKey(AccessKey _key) {
* @param _key the AccessKey
* @return the updated Application
*/
public Application removeAccessKey(AccessKey _key) {
public Application removeAccessKey(ExtendedAccessKey _key) {
return wrapped.removeAccessKey(_key)
.map((i) -> this)
.toBlocking()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.thethingsnetwork.account.sync.auth.grant;

import java.net.URI;
import org.thethingsnetwork.account.async.auth.grant.AsyncApplicationAccessKey;
import org.thethingsnetwork.account.async.auth.token.AsyncAccessKey;
import org.thethingsnetwork.account.common.GrantType;
import org.thethingsnetwork.account.sync.auth.token.AccessKey;

/**
* This token provider uses application credentials (access-key) to generate a token only usable for the owning application.
* It will always use access keys for authentication.
*
* @author Romain Cambier
*/
public class ApplicationAccessKey extends GrantType {

private final AsyncApplicationAccessKey wrapped;

/**
* Create an instance of this token provider using fully-customized settings
*
* @param _key The application access-key
* @param _accountServer The account server to be used
*/
public ApplicationAccessKey(String _key, URI _accountServer) {
wrapped = new AsyncApplicationAccessKey(_key, _accountServer);
}

/**
* Create an instance of this token provider using default account server
*
* @param _key The application access-key
*/
public ApplicationAccessKey(String _key) {
wrapped = new AsyncApplicationAccessKey(_key, GrantType.DEFAULT_ACCOUNT_SERVER);
}

@Override
public URI getAccountServer() {
return wrapped.getAccountServer();
}

/**
* Create a token using the settings provided in the constructor
*
* @return the AccessKey
*/
public AccessKey getToken() {
return wrapped.getToken()
.map((AsyncAccessKey t) -> new AccessKey(t))
.toBlocking()
.single();
}

}
Loading

0 comments on commit 030904c

Please sign in to comment.