Skip to content

Commit

Permalink
update header managment
Browse files Browse the repository at this point in the history
  • Loading branch information
BrapiCoordinatorSelby committed Jun 22, 2020
1 parent 04a5301 commit fdc7b48
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

import com.google.api.client.json.JsonFactory;
Expand All @@ -32,7 +35,7 @@ public class BrapiTestServerJWTAuthFilter extends BasicAuthenticationFilter {

private static final List<String> USER_IDS = Arrays.asList("dummy", "dummyAdmin", "113212610256718182401");
private static final List<String> ADMIN_IDS = Arrays.asList("dummyAdmin", "113212610256718182401");

public BrapiTestServerJWTAuthFilter(AuthenticationManager authManager) {
super(authManager);
}
Expand All @@ -55,26 +58,29 @@ protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res,
null, authorities);

SecurityContextHolder.getContext().setAuthentication(authentication);
}else {
throw new GeneralSecurityException("Auth Error");
}
} catch (GeneralSecurityException e) {
e.printStackTrace();
res.addHeader("WWW-Authenticate", "Basic realm=\"\"");
res.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
}

chain.doFilter(req, res);
}

private String checkAuthentication(HttpServletRequest req) throws FileNotFoundException, IOException, GeneralSecurityException {
private String checkAuthentication(HttpServletRequest req)
throws FileNotFoundException, IOException, GeneralSecurityException {
String userId = checkDummyAuthentication(req);
if (userId == null) {
checkGoogleAuthentication(req);
userId = checkGoogleAuthentication(req);
}
return userId;
}

private List<GrantedAuthority> getAuthorities(String userId) {
List<GrantedAuthority> auth = new ArrayList<>();
if (userId != null) {
if(USER_IDS.contains(userId)) {
if (USER_IDS.contains(userId)) {
GrantedAuthority user = new SimpleGrantedAuthority("USER");
auth.add(user);
}
Expand All @@ -86,38 +92,41 @@ private List<GrantedAuthority> getAuthorities(String userId) {
return auth;
}

private String checkGoogleAuthentication(HttpServletRequest request)
throws FileNotFoundException, IOException, GeneralSecurityException {
String token = request.getHeader("Authorization");
if (token != null) {
HttpTransport transport = new ApacheHttpTransport();
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jacksonFactory)
.setAudience(Collections
.singletonList("408930718026-1m4t6slfmp8c0vu0a4s0sp4ujvv3vqfa.apps.googleusercontent.com"))
// Or, if multiple clients access the backend:
// .setAudience(Arrays.asList(CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3))
.build();

GoogleIdToken idToken = verifier.verify(token.replace("Bearer ", ""));
if (idToken != null) {
Payload payload = idToken.getPayload();

// Print user identifier
String userId = payload.getSubject();
System.out.println("User ID: " + userId);
return userId;
private String checkGoogleAuthentication(HttpServletRequest request) {
try {
String token = request.getHeader("Authorization");
if (token != null) {
HttpTransport transport = new ApacheHttpTransport();
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, jacksonFactory)
.setAudience(Collections.singletonList(
"408930718026-1m4t6slfmp8c0vu0a4s0sp4ujvv3vqfa.apps.googleusercontent.com"))
// Or, if multiple clients access the backend:
// .setAudience(Arrays.asList(CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3))
.build();

GoogleIdToken idToken = verifier.verify(token.replace("Bearer ", ""));
if (idToken != null) {
Payload payload = idToken.getPayload();

// Print user identifier
String userId = payload.getSubject();
System.out.println("User ID: " + userId);
return userId;
}
return null;
}
return null;
} catch (Exception e) {
return null;
}
return null;
}

private String checkDummyAuthentication(HttpServletRequest request) {
String token = request.getHeader("Authorization");
if (token != null) {
if(token.equals("Bearer XXXX")) {
if (token.equals("Bearer XXXX")) {
return "dummy";
}else if(token.equals("Bearer YYYY")) {
} else if (token.equals("Bearer YYYY")) {
return "dummyAdmin";
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public ResponseEntity<ObservationUnitListResponse> observationunitsPut(
return responseOK(new ObservationUnitListResponse(), new ObservationUnitListResponseResult(), data);
}

public ResponseEntity<ObservationUnitTableResponse> observationunitsTableGet(
public ResponseEntity observationunitsTableGet(
@RequestHeader(value = "Accept", required = false) String accept,
@Valid @RequestParam(value = "observationUnitDbId", required = false) String observationUnitDbId,
@Valid @RequestParam(value = "germplasmDbId", required = false) String germplasmDbId,
Expand All @@ -140,11 +140,23 @@ public ResponseEntity<ObservationUnitTableResponse> observationunitsTableGet(
throws BrAPIServerException {

log.debug("Request: " + request.getRequestURI());
validateAcceptHeader(request);
ObservationUnitTable data = observationUnitService.findObservationUnitsTable(accept, observationUnitDbId,
String sep = "";
if("text/csv".equals(accept)) {
sep = ",";
}else if("text/tsv".equals(accept)) {
sep = "\t";
}else {
validateAcceptHeader(request);
}
ObservationUnitTable data = observationUnitService.findObservationUnitsTable(observationUnitDbId,
germplasmDbId, observationVariableDbId, studyDbId, locationDbId, trialDbId, programDbId, seasonDbId,
observationLevel);
return responseOK(new ObservationUnitTableResponse(), data);
if(sep.isEmpty()) {
return responseOK(new ObservationUnitTableResponse(), data);
}else {
String textTable = observationUnitService.getObservationUnitTableText(data, sep);
return new ResponseEntity<String>(textTable, HttpStatus.OK);
}
}

public ResponseEntity<ObservationUnitListResponse> searchObservationunitsPost(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.swagger.model.pheno.ObservationSingleResponse;
import io.swagger.model.pheno.ObservationTable;
import io.swagger.model.pheno.ObservationTableResponse;
import io.swagger.model.pheno.ObservationUnitTableResponse;

import java.time.OffsetDateTime;

import io.swagger.api.pheno.ObservationsApi;
Expand Down Expand Up @@ -120,7 +122,7 @@ public ResponseEntity<ObservationListResponse> observationsPut(
return responseOK(new ObservationListResponse(), new ObservationListResponseResult(), data);
}

public ResponseEntity<ObservationTableResponse> observationsTableGet(
public ResponseEntity observationsTableGet(
@RequestHeader(value = "Accept", required = false) String accept,
@Valid @RequestParam(value = "observationUnitDbId", required = false) String observationUnitDbId,
@Valid @RequestParam(value = "germplasmDbId", required = false) String germplasmDbId,
Expand All @@ -137,11 +139,23 @@ public ResponseEntity<ObservationTableResponse> observationsTableGet(
@RequestHeader(value = "Authorization", required = false) String authorization) throws BrAPIServerException {

log.debug("Request: " + request.getRequestURI());
validateAcceptHeader(request);
ObservationTable data = observationService.findObservationsTable(accept, observationUnitDbId, germplasmDbId,
String sep = "";
if("text/csv".equals(accept)) {
sep = ",";
}else if("text/tsv".equals(accept)) {
sep = "\t";
}else {
validateAcceptHeader(request);
}
ObservationTable data = observationService.findObservationsTable(observationUnitDbId, germplasmDbId,
observationVariableDbId, studyDbId, locationDbId, trialDbId, programDbId, seasonDbId,
observationTimeStampRangeStart, observationTimeStampRangeEnd);
return responseOK(new ObservationTableResponse(), data);
if(sep.isEmpty()) {
return responseOK(new ObservationTableResponse(), data);
}else {
String textTable = observationService.getObservationTableText(data, sep);
return new ResponseEntity<String>(textTable, HttpStatus.OK);
}
}

public ResponseEntity<ObservationListResponse> searchObservationsPost(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public List<Observation> findObservations(String observationDbId, String observa
return findObservations(request, metadata);
}

public ObservationTable findObservationsTable(String accept, String observationUnitDbId, String germplasmDbId,
public ObservationTable findObservationsTable(String observationUnitDbId, String germplasmDbId,
String observationVariableDbId, String studyDbId, String locationDbId, String trialDbId, String programDbId,
String seasonDbId, OffsetDateTime observationTimeStampRangeStart,
OffsetDateTime observationTimeStampRangeEnd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public List<ObservationUnit> findObservationUnits(String observationUnitDbId, St
return findObservationUnits(request, metadata);
}

public ObservationUnitTable findObservationUnitsTable(String accept, String observationUnitDbId,
public ObservationUnitTable findObservationUnitsTable(String observationUnitDbId,
String germplasmDbId, String observationVariableDbId, String studyDbId, String locationDbId,
String trialDbId, String programDbId, String seasonDbId, String observationLevel) {
List<ObservationUnit> observationUnits = findObservationUnits(null, germplasmDbId, studyDbId, locationDbId,
Expand Down

0 comments on commit fdc7b48

Please sign in to comment.