- public void clearToken(final char kind, final String ... scopes) throws CadiException {
- final String scope = addScope(scopes);
- char c;
- if(kind==Kind.OAUTH) {
- c = okind;
- } else {
- c = kind;
- }
- final String key = TokenClientFactory.getKey(c,client_id,username,hash,scope);
- factory.delete(key);
- }
- /**
- * Get AuthToken
- * @throws APIException
- * @throws CadiException
- * @throws LocatorException
- */
- public Result<TimedToken> getToken(final char kind, final String ... scopes) throws LocatorException, CadiException, APIException {
- final String scope = addScope(scopes);
- char c;
- if(kind==Kind.OAUTH) {
- c = okind;
- } else {
- c = kind;
- }
- final String key = TokenClientFactory.getKey(c,client_id,username,hash,scope);
- if(ss==null) {
- throw new APIException("client_creds(...) must be set before obtaining Access Tokens");
- }
-
- Result<TimedToken> rtt = factory.get(key,hash,new Loader<TimedToken>() {
- @Override
- public Result<TimedToken> load(final String key) throws APIException, CadiException, LocatorException {
- final List<String> params = new ArrayList<String>();
- params.add(scope);
- addSecurity(params,authn_method);
-
- final String paramsa[] = new String[params.size()];
- params.toArray(paramsa);
- Result<Token> rt = tkCon.best(new Retryable<Result<Token>>() {
- @Override
- public Result<Token> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
- // /token?grant_type=client_credential&scope=com.att.aaf+com.att.test
- Future<Token> f = client.postForm(null,tokenDF,paramsa);
- if(f.get(timeout)) {
- return Result.ok(f.code(),f.value);
- } else {
- return Result.err(f.code(), f.body());
- }
- }
- });
-
- if(rt.isOK()) {
- try {
- return Result.ok(rt.code,factory.putTimedToken(key,rt.value, hash));
- } catch (IOException e) {
- // TODO What to do here?
- e.printStackTrace();
- return Result.err(999,e.getMessage());
- }
- } else {
- return Result.err(rt);
- }
- }
- });
- if(rtt.isOK()) { // not validated for Expired
- TimedToken tt = rtt.value;
- if(tt.expired()) {
- rtt = refreshToken(tt);
- if(rtt.isOK()) {
- tkCon.access.printf(Level.INFO, "Refreshed token %s to %s",tt.getAccessToken(),rtt.value.getAccessToken());
- return Result.ok(200,rtt.value);
- } else {
- tkCon.access.printf(Level.INFO, "Expired token %s cannot be renewed %d %s",tt.getAccessToken(),rtt.code,rtt.error);
- factory.delete(key);
- tt=null;
- }
- } else {
- return Result.ok(200,tt);
- }
- } else {
- Result.err(rtt);
- }
- return Result.err(404,"Not Found");
- }
-
- public Result<TimedToken> refreshToken(Token token) throws APIException, LocatorException, CadiException {
- if(ss==null) {
- throw new APIException("client_creds(...) must be set before obtaining Access Tokens");
- }
- final List<String> params = new ArrayList<String>();
- params.add("refresh_token="+token.getRefreshToken());
- addSecurity(params,AUTHN_METHOD.refresh_token);
- final String scope="scope="+token.getScope().replace(' ', '+');
- params.add(scope);
-
- final String paramsa[] = new String[params.size()];
- params.toArray(paramsa);
- Result<Token> rt = tkCon.best(new Retryable<Result<Token>>() {
- @Override
- public Result<Token> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
- // /token?grant_type=client_credential&scope=com.att.aaf+com.att.test
- Future<Token> f = client.postForm(null,tokenDF,paramsa);
- if(f.get(timeout)) {
- return Result.ok(f.code(),f.value);
- } else {
- return Result.err(f.code(), f.body());
- }
- }
- });
- String key = TokenClientFactory.getKey(okind,client_id, username, hash, scope);
- if(rt.isOK()) {
- try {
- return Result.ok(200,factory.putTimedToken(key, rt.value, hash));
- } catch (IOException e) {
- //TODO what to do here?
- return Result.err(999, e.getMessage());
- }
- } else if(rt.code==404) {
- factory.deleteFromDisk(key);
- }
- return Result.err(rt);
- }
+ public void clearToken(final char kind, final String ... scopes) throws CadiException {
+ final String scope = addScope(scopes);
+ char c;
+ if (kind==Kind.OAUTH) {
+ c = okind;
+ } else {
+ c = kind;
+ }
+ final String key = TokenClientFactory.getKey(c,client_id,username,hash,scope);
+ factory.delete(key);
+ }
+ /**
+ * Get AuthToken
+ * @throws APIException
+ * @throws CadiException
+ * @throws LocatorException
+ */
+ public Result<TimedToken> getToken(final char kind, final String ... scopes) throws LocatorException, CadiException, APIException {
+ final String scope = addScope(scopes);
+ char c;
+ if (kind==Kind.OAUTH) {
+ c = okind;
+ } else {
+ c = kind;
+ }
+ final String key = TokenClientFactory.getKey(c,client_id,username,hash,scope);
+ if (ss==null) {
+ throw new APIException("client_creds(...) must be set before obtaining Access Tokens");
+ }
+
+ Result<TimedToken> rtt = factory.get(key,hash,new Loader<TimedToken>() {
+ @Override
+ public Result<TimedToken> load(final String key) throws APIException, CadiException, LocatorException {
+ final List<String> params = new ArrayList<>();
+ params.add(scope);
+ addSecurity(params,authn_method);
+
+ final String paramsa[] = new String[params.size()];
+ params.toArray(paramsa);
+ Result<Token> rt = tkCon.best(new Retryable<Result<Token>>() {
+ @Override
+ public Result<Token> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
+ // /token?grant_type=client_credential&scope=com.att.aaf+com.att.test
+ Future<Token> f = client.postForm(null,tokenDF,paramsa);
+ if (f.get(timeout)) {
+ return Result.ok(f.code(),f.value);
+ } else {
+ return Result.err(f.code(), f.body());
+ }
+ }
+ });
+
+ if (rt.isOK()) {
+ try {
+ return Result.ok(rt.code,factory.putTimedToken(key,rt.value, hash));
+ } catch (IOException e) {
+ // TODO What to do here?
+ e.printStackTrace();
+ return Result.err(999,e.getMessage());
+ }
+ } else {
+ return Result.err(rt);
+ }
+ }
+ });
+ if (rtt.isOK()) { // not validated for Expired
+ TimedToken tt = rtt.value;
+ if (tt.expired()) {
+ rtt = refreshToken(tt);
+ if (rtt.isOK()) {
+ tkCon.access.printf(Level.INFO, "Refreshed token %s to %s",tt.getAccessToken(),rtt.value.getAccessToken());
+ return Result.ok(200,rtt.value);
+ } else {
+ tkCon.access.printf(Level.INFO, "Expired token %s cannot be renewed %d %s",tt.getAccessToken(),rtt.code,rtt.error);
+ factory.delete(key);
+ tt=null;
+ }
+ } else {
+ return Result.ok(200,tt);
+ }
+ } else {
+ Result.err(rtt);
+ }
+ return Result.err(404,"Not Found");
+ }
+
+ public Result<TimedToken> refreshToken(Token token) throws APIException, LocatorException, CadiException {
+ if (ss==null) {
+ throw new APIException("client_creds(...) must be set before obtaining Access Tokens");
+ }
+ final List<String> params = new ArrayList<>();
+ params.add("refresh_token="+token.getRefreshToken());
+ addSecurity(params,AUTHN_METHOD.refresh_token);
+ final String scope="scope="+token.getScope().replace(' ', '+');
+ params.add(scope);
+
+ final String paramsa[] = new String[params.size()];
+ params.toArray(paramsa);
+ Result<Token> rt = tkCon.best(new Retryable<Result<Token>>() {
+ @Override
+ public Result<Token> code(Rcli<?> client) throws CadiException, ConnectException, APIException {
+ // /token?grant_type=client_credential&scope=com.att.aaf+com.att.test
+ Future<Token> f = client.postForm(null,tokenDF,paramsa);
+ if (f.get(timeout)) {
+ return Result.ok(f.code(),f.value);
+ } else {
+ return Result.err(f.code(), f.body());
+ }
+ }
+ });
+ String key = TokenClientFactory.getKey(okind,client_id, username, hash, scope);
+ if (rt.isOK()) {
+ try {
+ return Result.ok(200,factory.putTimedToken(key, rt.value, hash));
+ } catch (IOException e) {
+ //TODO what to do here?
+ return Result.err(999, e.getMessage());
+ }
+ } else if (rt.code==404) {
+ factory.deleteFromDisk(key);
+ }
+ return Result.err(rt);
+ }