use of ch.cyberduck.core.Credentials in project cyberduck by iterate-ch.
the class BoxSession method login.
@Override
public void login(final Proxy proxy, final LoginCallback prompt, final CancelCallback cancel) throws BackgroundException {
authorizationService.setTokens(authorizationService.authorize(host, prompt, cancel, OAuth2AuthorizationService.FlowType.AuthorizationCode));
try {
final Credentials credentials = host.getCredentials();
credentials.setUsername(new UsersApi(new BoxApiClient(client)).getUsersMe(Collections.emptyList()).getLogin());
credentials.setSaved(true);
} catch (ApiException e) {
throw new BoxExceptionMappingService(fileid).map(e);
}
}
use of ch.cyberduck.core.Credentials in project cyberduck by iterate-ch.
the class BrickPairingSchedulerFeature method operate.
/**
* Pool for pairing key from service
*
* @param callback Callback when service returns 200
*/
private void operate(final PasswordCallback callback) throws BackgroundException {
try {
final HttpPost resource = new HttpPost(String.format("%s/api/rest/v1/sessions/pairing_key/%s", new HostUrlProvider().withUsername(false).withPath(false).get(session.getHost()), token));
resource.setHeader(HttpHeaders.ACCEPT, "application/json");
resource.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
if (log.isInfoEnabled()) {
log.info(String.format("Fetch credentials for paring key %s from %s", token, resource));
}
final JsonObject json = session.getClient().execute(resource, new AbstractResponseHandler<JsonObject>() {
@Override
public JsonObject handleEntity(final HttpEntity entity) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(entity.getContent(), out);
return JsonParser.parseReader(new InputStreamReader(new ByteArrayInputStream(out.toByteArray()))).getAsJsonObject();
}
});
if (json.has("nickname")) {
if (new HostPreferences(session.getHost()).getBoolean("brick.pairing.nickname.configure")) {
final JsonPrimitive nickname = json.getAsJsonPrimitive("nickname");
if (StringUtils.isNotBlank(host.getNickname())) {
if (!StringUtils.equals(host.getNickname(), nickname.getAsString())) {
log.warn(String.format("Mismatch of nickname. Previously authorized as %s and now paired as %s", host.getNickname(), nickname.getAsString()));
callback.close(null);
throw new LoginCanceledException();
}
}
host.setNickname(nickname.getAsString());
}
}
final Credentials credentials = host.getCredentials();
if (json.has("user_username")) {
credentials.setUsername(json.getAsJsonPrimitive("user_username").getAsString());
} else {
throw new LoginFailureException(String.format("Invalid response for pairing key %s", token));
}
if (json.has("password")) {
credentials.setPassword(json.getAsJsonPrimitive("password").getAsString());
} else {
throw new LoginFailureException(String.format("Invalid response for pairing key %s", token));
}
if (json.has("server")) {
if (new HostPreferences(session.getHost()).getBoolean("brick.pairing.hostname.configure")) {
host.setHostname(URI.create(json.getAsJsonPrimitive("server").getAsString()).getHost());
}
}
callback.close(credentials.getUsername());
} catch (JsonParseException e) {
throw new DefaultIOExceptionMappingService().map(new IOException(e.getMessage(), e));
} catch (HttpResponseException e) {
switch(e.getStatusCode()) {
case HttpStatus.SC_NOT_FOUND:
log.warn(String.format("Missing login for pairing key %s", token));
cancel.verify();
break;
default:
throw new DefaultHttpResponseExceptionMappingService().map(e);
}
} catch (IOException e) {
throw new DefaultIOExceptionMappingService().map(e);
}
}
use of ch.cyberduck.core.Credentials in project cyberduck by iterate-ch.
the class AbstractBrickTest method setup.
@Before
public void setup() throws Exception {
final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new BrickProtocol())));
final Profile profile = new ProfilePlistReader(factory).read(this.getClass().getResourceAsStream("/Brick.cyberduckprofile"));
final Host host = new Host(profile, "mountainduck.files.com", new Credentials(System.getProperties().getProperty("brick.user"), System.getProperties().getProperty("brick.password")));
session = new BrickSession(host, new DefaultX509TrustManager(), new DefaultX509KeyManager());
final LoginConnectionService login = new LoginConnectionService(new DisabledLoginCallback() {
@Override
public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) {
fail(reason);
return null;
}
}, new DisabledHostKeyCallback(), new DisabledPasswordStore(), new DisabledProgressListener());
login.check(session, new DisabledCancelCallback());
}
use of ch.cyberduck.core.Credentials in project cyberduck by iterate-ch.
the class AbtractBoxTest method setup.
@Before
public void setup() throws Exception {
final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new BoxProtocol())));
final Profile profile = new ProfilePlistReader(factory).read(this.getClass().getResourceAsStream("/Box.cyberduckprofile"));
final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials("cyberduck"));
session = new BoxSession(host, new DefaultX509TrustManager(), new DefaultX509KeyManager());
final LoginConnectionService login = new LoginConnectionService(new DisabledLoginCallback() {
@Override
public Credentials prompt(final Host bookmark, final String title, final String reason, final LoginOptions options) {
fail(reason);
return null;
}
}, new DisabledHostKeyCallback(), new TestPasswordStore(), new DisabledProgressListener());
login.check(session, new DisabledCancelCallback());
}
use of ch.cyberduck.core.Credentials in project cyberduck by iterate-ch.
the class CallbackProxyAuthenticationStrategy method authSucceeded.
@Override
public void authSucceeded(final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final Credentials credentials = clientContext.getAttribute(PROXY_CREDENTIALS_INPUT_ID, Credentials.class);
if (null != credentials) {
clientContext.removeAttribute(PROXY_CREDENTIALS_INPUT_ID);
if (log.isInfoEnabled()) {
log.info(String.format("Save passphrase for proxy %s", authhost));
}
keychain.addCredentials(authhost.toURI(), credentials.getUsername(), credentials.getPassword());
}
super.authSucceeded(authhost, authScheme, context);
}
Aggregations