gpt4 book ai didi

com.github.robozonky.api.remote.entities.ZonkyApiToken类的使用及代码示例

转载 作者:知者 更新时间:2024-03-14 05:35:31 25 4
gpt4 key购买 nike

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

ZonkyApiToken介绍

[英]OAuth access token for Zonky API.

Knowledge of this token will allow anyone to access the service as if they were the authenticated user. This is therefore highly sensitive information and should never be kept in memory for longer than necessary.
[中]Zonky API的OAuth访问令牌。
了解此令牌将允许任何人访问服务,就像他们是经过身份验证的用户一样。因此,这是高度敏感的信息,不应在内存中保存超过必要的时间。

代码示例

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

@Override
  public void filter(final ClientRequestContext clientRequestContext) {
    final ZonkyApiToken supplied = token == null ? null : token.get();
    logger.trace("Using token #{}.", supplied);
    final char[] t = supplied == null ? AuthenticatedFilter.EMPTY_TOKEN : supplied.getAccessToken();
    this.setRequestHeader("Authorization", "Bearer " + String.valueOf(t));
    super.filter(clientRequestContext);
  }
}

代码示例来源: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

public boolean isExpired() {
  return willExpireIn(Duration.ZERO);
}

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

public ZonkyApiToken refresh(final ZonkyApiToken token) {
    return api.call(a -> a.refresh(String.valueOf(token.getRefreshToken()), ZonkyApiToken.REFRESH_TOKEN_STRING,
                    token.getScope()));
  }
}

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

private static ZonkyApiToken getTokenExpiringIn(final Duration duration) {
  return new ZonkyApiToken(UUID.randomUUID().toString(), UUID.randomUUID().toString(),
               OffsetDateTime.ofInstant(DateUtil.now().minus(Duration.ofMinutes(5)).plus(duration),
                           Defaults.ZONE_ID));
}

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

@Test
void fresh() {
  final ZonkyApiToken token = new ZonkyApiToken();
  assertThat(token.getObtainedOn()).isBeforeOrEqualTo(OffsetDateTime.now());
}

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

public boolean willExpireIn(final TemporalAmount temporalAmount) {
  final OffsetDateTime maxExpirationDate = OffsetDateTime.now().plus(temporalAmount);
  return getExpiresOn().isBefore(maxExpirationDate);
}

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

static Duration reloadAfter(final ZonkyApiToken token) {
  final int expirationInSeconds = token.getExpiresIn();
  final int minimumSecondsBeforeExpiration = 5;
  final int secondsToReloadAfter =
      Math.max(minimumSecondsBeforeExpiration, expirationInSeconds - minimumSecondsBeforeExpiration);
  return Duration.ofSeconds(secondsToReloadAfter);
}

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

@Test
  void refresh() {
    final String originalTokenId = UUID.randomUUID().toString();
    final ZonkyApiToken originToken = new ZonkyApiToken(UUID.randomUUID().toString(), originalTokenId,
                              OffsetDateTime.now());
    final ZonkyApiToken resultToken = mock(ZonkyApiToken.class);
    final ZonkyOAuthApi api = mock(ZonkyOAuthApi.class);
    when(api.refresh(eq(originalTokenId), anyString(), any())).thenReturn(resultToken);
    final Api<ZonkyOAuthApi> wrapper = new Api<>(api);
    final OAuth oauth = new OAuth(wrapper);
    final ZonkyApiToken returnedToken = oauth.refresh(originToken);
    assertThat(returnedToken).isEqualTo(resultToken);
  }
}

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

public ZonkyApiToken refresh(final ZonkyApiToken token) {
    final OAuthScope scope = token.getScope().getPrimaryScope().orElse(OAuthScope.SCOPE_APP_WEB);
    return api.call(a -> a.refresh(String.valueOf(token.getRefreshToken()), ZonkyApiToken.REFRESH_TOKEN_STRING,
                    scope));
  }
}

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

public boolean willExpireIn(final TemporalAmount temporalAmount) {
  final OffsetDateTime maxExpirationDate = DateUtil.offsetNow().plus(temporalAmount);
  return getExpiresOn().isBefore(maxExpirationDate);
}

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

@Override
  public void filter(final ClientRequestContext clientRequestContext) {
    final ZonkyApiToken supplied = token == null ? null : token.get();
    final char[] t = supplied == null ? AuthenticatedFilter.EMPTY_TOKEN : supplied.getAccessToken();
    this.setRequestHeader("Authorization", "Bearer " + String.valueOf(t));
    super.filter(clientRequestContext);
  }
}

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

@Test
  void changes400to401() throws IOException {
    final int expectedCode = 400;
    final ClientRequestContext ctx = mock(ClientRequestContext.class);
    final ClientResponseContext ctx2 = mock(ClientResponseContext.class);
    final ZonkyApiToken token = new ZonkyApiToken("", "", 299);
    when(ctx2.hasEntity()).thenReturn(true);
    when(ctx2.getHeaders()).thenReturn(new MultivaluedMapImpl<>());
    when(ctx2.getEntityStream()).thenReturn(c(token));
    when(ctx2.getStatusInfo()).thenReturn(Response.Status.fromStatusCode(expectedCode));
    when(ctx2.getStatus()).thenReturn(expectedCode);
    final RoboZonkyFilter filter = new AuthenticatedFilter(() -> token);
    filter.filter(ctx, ctx2);
    verify(ctx2, times(1)).setStatus(401);
  }
}

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

@Override
  public synchronized void close() {
    final ZonkyApiToken toClose = token.getAndSet(null);
    if (toClose == null || toClose.willExpireIn(Duration.ZERO)) {
      return;
    }
    LOGGER.debug("Logging '{}' out of Zonky ({}).", secrets.getUsername(), scope);
    apis.run(Zonky::logout, () -> toClose);
  }
}

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

private static InputStream c(final ZonkyApiToken token) {
  final String error = "{\"error\":\"invalid_token\",\"error_description\":\"Invalid access token: "
      + Arrays.toString(token.getAccessToken()) + "\"}";
  return new ByteArrayInputStream(error.getBytes(Defaults.CHARSET));
}

代码示例来源: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 static ZonkyApiToken mockToken() {
  final ZonkyApiToken t = mock(ZonkyApiToken.class);
  when(t.getAccessToken()).thenReturn(UUID.randomUUID().toString().toCharArray());
  return t;
}

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

@SuppressWarnings({"unchecked", "rawtypes"})
private ApiProvider mockApi(final String username, final char... password) {
  final ApiProvider api = spy(new ApiProvider());
  final ZonkyApiToken token = mock(ZonkyApiToken.class);
  when(token.getAccessToken()).thenReturn(new char[0]);
  final OAuth oauth = mock(OAuth.class);
  when(oauth.login(eq(username), eq(password))).thenReturn(token);
  doAnswer(i -> {
    final Function f = i.getArgument(0);
    return f.apply(oauth);
  }).when(api).oauth(any());
  final Zonky z = mock(Zonky.class);
  doAnswer(i -> {
    final Consumer f = i.getArgument(0);
    f.accept(z);
    return null;
  }).when(api).run(any(Consumer.class), any());
  return api;
}

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

@SuppressWarnings({"unchecked", "rawtypes"})
private ApiProvider mockFailingApi() {
  final ApiProvider api = spy(new ApiProvider());
  final ZonkyApiToken token = mock(ZonkyApiToken.class);
  when(token.getAccessToken()).thenReturn(new char[0]);
  final OAuth oauth = mock(OAuth.class);
  when(oauth.login(any(), any())).thenReturn(token);
  doAnswer(i -> {
    final Function f = i.getArgument(0);
    return f.apply(oauth);
  }).when(api).oauth(any());
  final Zonky z = mock(Zonky.class);
  doAnswer(i -> {
    final Consumer f = i.getArgument(0);
    f.accept(z);
    return null;
  }).when(api).run(any(Consumer.class), any());
  doThrow(IllegalStateException.class).when(z).logout(); // last call will fail
  return api;
}

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

@Test
void hasToken() throws URISyntaxException {
  final ClientRequestContext crc = mock(ClientRequestContext.class);
  when(crc.getUri()).thenReturn(new URI("http://somewhere"));
  when(crc.getHeaders()).thenReturn(new MultivaluedHashMap<>());
  getTestedFilter().filter(crc);
  assertThat(crc.getHeaders().getFirst("Authorization"))
      .isEqualTo("Bearer " + String.valueOf(AuthenticatedFilterTest.TOKEN.getAccessToken()));
}

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