Skip to content

Commit

Permalink
Merge pull request #72 from RADAR-base/fix_unauthorised
Browse files Browse the repository at this point in the history
Checks for authorization validity when filtering users
  • Loading branch information
yatharthranjan authored Oct 20, 2020
2 parents 56058d7 + 97340a5 commit b83b721
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ protected Duration getLookbackTime() {
*/
protected Instant nextPoll(User user) {
Instant offset = getOffset(user);
if (offset.isAfter(user.getEndDate())) {
if (offset.isAfter(user.getEndDate().minus(getEndDateThreshold()))) {
return nearFuture();
} else {
Instant nextPoll = lastPollPerUser.getOrDefault(user.getId(), MIN_INSTANT)
Expand All @@ -321,6 +321,10 @@ protected Instant nextPoll(User user) {
}
}

private TemporalAmount getEndDateThreshold() {
return Duration.ofHours(1);
}

/**
* Generate one date per day, using UTC time zone. The first date will have the time from the
* given startDate. Following time stamps will start at 00:00. This will not up to the date of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class LocalUser implements User {
@JsonProperty("oauth2")
private OAuth2UserCredentials oauth2Credentials = new OAuth2UserCredentials();

@JsonProperty("authorized")
private Boolean isAuthorized;

@JsonIgnore
private SchemaAndValue observationKey;

Expand Down Expand Up @@ -95,6 +98,15 @@ public void setFitbitUserId(String id) {
this.externalUserId = id;
}

@Override
public boolean isAuthorized() {
if(isAuthorized == null) {
return !oauth2Credentials.isAccessTokenExpired()
|| oauth2Credentials.hasRefreshToken();
}
return isAuthorized;
}

@Override
public String getVersion() {
return version;
Expand All @@ -115,6 +127,7 @@ public LocalUser copy() {
copy.endDate = endDate;
copy.sourceId = sourceId;
copy.oauth2Credentials = oauth2Credentials;
copy.isAuthorized = isAuthorized;
return copy;
}

Expand All @@ -133,6 +146,7 @@ public String toString() {
+ ", projectId='" + projectId + '\''
+ ", userId='" + userId + '\''
+ ", sourceId='" + sourceId + '\''
+ ", isAuthorized='" + isAuthorized() + '\''
+ '}';
}

Expand All @@ -152,7 +166,8 @@ public boolean equals(Object o) {
&& Objects.equals(userId, localUser.userId)
&& Objects.equals(sourceId, localUser.sourceId)
&& Objects.equals(startDate, localUser.startDate)
&& Objects.equals(endDate, localUser.endDate);
&& Objects.equals(endDate, localUser.endDate)
&& Objects.equals(isAuthorized(), localUser.isAuthorized());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ static SchemaAndValue computeObservationKey(AvroData avroData, User user) {

String getSourceId();

boolean isAuthorized();

default String getVersionedId() {
String version = getVersion();
if (version == null) {
Expand All @@ -60,6 +62,7 @@ default Boolean isComplete() {
return getEndDate() != null
&& getStartDate() != null
&& getProjectId() != null
&& getUserId() != null;
&& getUserId() != null
&& isAuthorized();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public String getSourceId() {
return fitbitAuthDetails.getSourceId();
}

@Override
public boolean isAuthorized() {
return !fitbitAuthDetails.getOauth2Credentials().isAccessTokenExpired()
|| fitbitAuthDetails.getOauth2Credentials().hasRefreshToken();
}

public FirebaseUserDetails getFirebaseUserDetails() {
return firebaseUserDetails;
}
Expand Down

0 comments on commit b83b721

Please sign in to comment.