gpt4 book ai didi

com.github.robozonky.app.tenant.ZonkyApiTokenSupplier类的使用及代码示例

转载 作者:知者 更新时间:2024-03-13 12:33:39 26 4
gpt4 key购买 nike

本文整理了Java中com.github.robozonky.app.tenant.ZonkyApiTokenSupplier类的一些代码示例,展示了ZonkyApiTokenSupplier类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZonkyApiTokenSupplier类的具体详情如下:
包路径:com.github.robozonky.app.tenant.ZonkyApiTokenSupplier
类名称:ZonkyApiTokenSupplier

ZonkyApiTokenSupplier介绍

[英]Will keep permanent user authentication running in the background.
[中]将保持永久用户身份验证在后台运行。

代码示例

代码示例来源:origin: com.github.robozonky/robozonky-app

@Override
public void close() { // cancel existing tokens
  tokens.forEach((k, v) -> v.close());
}

代码示例来源:origin: RoboZonky/robozonky

@Override
public boolean isAvailable(final OAuthScope scope) {
  // either Zonky is not available, or we have already logged out prior to daemon shutdown
  return availability.getAsBoolean() && !getTokenSupplier(scope).isClosed();
}

代码示例来源:origin: RoboZonky/robozonky

private ZonkyApiToken refreshOrLogin(final ZonkyApiToken token) {
  final ZonkyApiToken result = actuallyRefreshOrLogin(token);
  LOGGER.debug("Token changed from {} to {}.", token, result);
  return result;
}

代码示例来源:origin: com.github.robozonky/robozonky-app

private synchronized ZonkyApiToken getTokenInAnyWay(final ZonkyApiToken currentToken) {
  return currentToken == null ? login() : refreshTokenIfNecessary(currentToken);
}

代码示例来源:origin: com.github.robozonky/robozonky-app

private ZonkyApiToken refreshTokenIfNecessary(final ZonkyApiToken token) {
  if (!token.willExpireIn(refresh)) {
    return token;
  }
  LOGGER.debug("Token refresh commencing.");
  isUpdating.set(true);
  try {
    return refreshToken(token);
  } catch (final Exception ex) {
    LOGGER.debug("Failed refreshing access token, falling back to password.", ex);
    return login();
  } finally {
    isUpdating.set(false);
    LOGGER.debug("Token refresh over.");
  }
}

代码示例来源:origin: RoboZonky/robozonky

private ZonkyApiToken actuallyRefreshOrLogin(final ZonkyApiToken token) {
  if (token.isExpired()) {
    LOGGER.debug("Found expired token #{}.", token.getId());
    return login();
  }
  LOGGER.debug("Current token #{} expiring on {}.", token.getId(), token.getExpiresOn());
  try {
    return refresh(token);
  } catch (final Exception ex) {
    LOGGER.debug("Failed refreshing access token, falling back to password.", ex);
    return login();
  }
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void closingLoaded() {
  final Zonky zonky = mock(Zonky.class);
  final OAuth oAuth = mock(OAuth.class);
  final ZonkyApiToken token = getTokenExpiringIn(Duration.ofSeconds(5));
  when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
      .thenAnswer(invocation -> token);
  when(oAuth.refresh(any())).thenReturn(token);
  final ApiProvider api = mockApi(oAuth, zonky);
  final ZonkyApiTokenSupplier t = new ZonkyApiTokenSupplier(api, SECRETS);
  t.get();
  verify(oAuth).login(any(), any(), any());
  assertThat(t.isClosed()).isFalse();
  t.close();
  verify(zonky, only()).logout();
  assertThat(t.isClosed()).isTrue();
  assertThatThrownBy(t::get).isInstanceOf(IllegalStateException.class);
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void refreshes() {
  final Zonky zonky = mock(Zonky.class);
  final OAuth oAuth = mock(OAuth.class);
  final ZonkyApiToken token = getTokenExpiringIn(Duration.ofMinutes(5));
  when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
      .thenAnswer(invocation -> token);
  final ApiProvider api = mockApi(oAuth, zonky);
  final ZonkyApiTokenSupplier t = new ZonkyApiTokenSupplier(api, SECRETS);
  assertThat(t.get()).isEqualTo(token);
  skipAheadBy(Duration.ofSeconds(4 * 60 + 56)); // get over the refresh period
  final ZonkyApiToken secondToken = getTokenExpiringIn(Duration.ofMinutes(5));
  when(oAuth.refresh(any())).thenReturn(secondToken);
  assertThat(t.get()).isEqualTo(secondToken);
}

代码示例来源:origin: RoboZonky/robozonky

@Test
  void notClosingWhenExpired() {
    final Zonky zonky = mock(Zonky.class);
    final OAuth oAuth = mock(OAuth.class);
    final ZonkyApiToken token = getTokenExpiringIn(Duration.ZERO);
    when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
        .thenAnswer(invocation -> token);
    final ApiProvider api = mockApi(oAuth, zonky);
    final ZonkyApiTokenSupplier t = new ZonkyApiTokenSupplier(api, SECRETS);
    t.close();
    verify(zonky, never()).logout();
  }
}

代码示例来源:origin: com.github.robozonky/robozonky-app

public PowerTenant build(final Duration tokenRefresh) {
  if (secrets == null) {
    throw new IllegalStateException("Secret provider must be provided.");
  }
  final ApiProvider apis = api == null ? new ApiProvider() : api;
  final Function<ZonkyScope, ZonkyApiTokenSupplier> tokenSupplier =
      scope -> new ZonkyApiTokenSupplier(scope, apis, secrets, tokenRefresh);
  final SessionInfo sessionInfo = new SessionInfo(secrets.getUsername(), name, dryRun);
  return new PowerTenantImpl(sessionInfo, apis, strategyProvider, tokenSupplier);
}

代码示例来源:origin: com.github.robozonky/robozonky-app

@Override
public boolean isAvailable(final ZonkyScope scope) {
  return getTokenSupplier(scope).isAvailable();
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void failsOnRefresh() {
  final Zonky zonky = mock(Zonky.class);
  final OAuth oAuth = mock(OAuth.class);
  when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
      .thenAnswer(invocation -> getTokenExpiringIn(Duration.ofMinutes(5)));
  final ApiProvider api = mockApi(oAuth, zonky);
  final ZonkyApiTokenSupplier t = new ZonkyApiTokenSupplier(api, SECRETS);
  final ZonkyApiToken token = t.get();
  assertThat(token).isNotNull();
  skipAheadBy(Duration.ofSeconds(4 * 60 + 55)); // get over the refresh period, but not over expiration
  doThrow(IllegalStateException.class).when(oAuth).refresh(any());
  assertThat(t.get())
      .isNotNull()
      .isNotSameAs(token);
  verify(oAuth).refresh(any()); // make sure refresh was rejected before login was called
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void closingNeverLoaded() {
  final Zonky zonky = mock(Zonky.class);
  final OAuth oAuth = mock(OAuth.class);
  final ZonkyApiToken token = getTokenExpiringIn(Duration.ofSeconds(5));
  when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
      .thenAnswer(invocation -> token);
  when(oAuth.refresh(any())).thenReturn(token);
  final ApiProvider api = mockApi(oAuth, zonky);
  final ZonkyApiTokenSupplier t = new ZonkyApiTokenSupplier(api, SECRETS);
  t.close();
  verify(oAuth, never()).login(any(), any(), any());
  verify(zonky, never()).logout();
  assertThatThrownBy(t::get).isInstanceOf(IllegalStateException.class);
}

代码示例来源:origin: RoboZonky/robozonky

public PowerTenant build() {
  if (secrets == null) {
    throw new IllegalStateException("Secret provider must be provided.");
  }
  final ApiProvider apis = api == null ? new ApiProvider() : api;
  final Function<OAuthScope, ZonkyApiTokenSupplier> tokenSupplier =
      scope -> new ZonkyApiTokenSupplier(scope, apis, secrets);
  final SessionInfo sessionInfo = new SessionInfo(secrets.getUsername(), name, dryRun);
  final BooleanSupplier zonkyAvailability = lifecycle == null ? () -> true : () -> lifecycle.get().isOnline();
  return new PowerTenantImpl(sessionInfo, apis, zonkyAvailability, strategyProvider, tokenSupplier);
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void newLoginWhenTokenExpiredWithoutRefresh() {
  final Zonky zonky = mock(Zonky.class);
  final OAuth oAuth = mock(OAuth.class);
  final ZonkyApiToken token = getTokenExpiringIn(Duration.ofMinutes(5));
  when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
      .thenAnswer(invocation -> token);
  final ApiProvider api = mockApi(oAuth, zonky);
  final ZonkyApiTokenSupplier t = new ZonkyApiTokenSupplier(api, SECRETS);
  assertThat(t.get()).isEqualTo(token);
  skipAheadBy(Duration.ofMinutes(6)); // get over the expiration period
  final ZonkyApiToken secondToken = getTokenExpiringIn(Duration.ofMinutes(5));
  when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
      .thenAnswer(invocation -> secondToken);
  assertThat(t.get()).isEqualTo(secondToken);
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void failsOnLogin() {
  final Zonky zonky = mock(Zonky.class);
  final OAuth oAuth = mock(OAuth.class);
  doThrow(IllegalStateException.class).when(oAuth).login(any(), any(), any());
  final ApiProvider api = mockApi(oAuth, zonky);
  final ZonkyApiTokenSupplier t = new ZonkyApiTokenSupplier(api, SECRETS);
  assertThatThrownBy(t::get).isInstanceOf(IllegalStateException.class);
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void availabilityOfToken() {
  final ZonkyApiTokenSupplier s = mock(ZonkyApiTokenSupplier.class);
  final PowerTenantImpl t = new PowerTenantImpl(SESSION_DRY, null, () -> true, null, scope -> s);
  assertThat(t.isAvailable(OAuthScope.SCOPE_APP_WEB)).isTrue();
  when(s.isClosed()).thenReturn(true);
  assertThat(t.isAvailable(OAuthScope.SCOPE_APP_WEB)).isFalse();
}

代码示例来源:origin: RoboZonky/robozonky

@Override
public void close() {
  tokens.forEach((k, v) -> v.close()); // cancel existing tokens
  loanCache.get().close(); // clean up the cache
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void reloginsWhenAlreadyExpired() {
  final Zonky zonky = mock(Zonky.class);
  final OAuth oAuth = mock(OAuth.class);
  final ZonkyApiToken token = getTokenExpiringIn(Duration.ofMinutes(5));
  when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
      .thenAnswer(invocation -> token);
  final ApiProvider api = mockApi(oAuth, zonky);
  final ZonkyApiTokenSupplier t = new ZonkyApiTokenSupplier(api, SECRETS);
  assertThat(t.get()).isEqualTo(token);
  skipAheadBy(Duration.ofMinutes(6)); // get over the expiration period
  final ZonkyApiToken secondToken = getTokenExpiringIn(Duration.ofMinutes(5));
  when(oAuth.login(eq(OAuthScope.SCOPE_APP_WEB), eq(SECRETS.getUsername()), eq(SECRETS.getPassword())))
      .thenAnswer(invocation -> secondToken);
  assertThat(t.get()).isEqualTo(secondToken);
}

代码示例来源:origin: RoboZonky/robozonky

@Test
void availabilityOfZonky() {
  final ZonkyApiTokenSupplier s = mock(ZonkyApiTokenSupplier.class);
  final PowerTenantImpl t = new PowerTenantImpl(SESSION_DRY, null, () -> false, null, scope -> s);
  assertThat(t.isAvailable(OAuthScope.SCOPE_APP_WEB)).isFalse();
  when(s.isClosed()).thenReturn(true); // token availability makes no difference
  assertThat(t.isAvailable(OAuthScope.SCOPE_APP_WEB)).isFalse();
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com