@@ -87,56 +87,38 @@ public void doFilter(ServletRequest req, ServletResponse res,
8787 if ((uri .indexOf ("/espi/1_1/resource/" ) != -1 ))
8888 {
8989 resourceRequest = true ;
90+ }
9091
91- ///////////////////////////////////////////////////////////////////////
92- // find the access token if present and validate we have a good one
93- ///////////////////////////////////////////////////////////////////////
94- String token = request .getHeader ("authorization" );
92+ ///////////////////////////////////////////////////////////////////////
93+ // find the access token if present and validate we have a good one
94+ ///////////////////////////////////////////////////////////////////////
95+ String token = request .getHeader ("authorization" );
9596
96- if (token !=null )
97+ if (token !=null )
98+ {
99+ if (token .contains ("Bearer" ))
97100 {
98- if (token .contains ("Bearer" ))
99- {
100- // has Authorization header with Bearer type
101- hasBearer = true ;
102- token = token .replace ("Bearer " , "" );
103-
104- // ensure length is >12 characters (48 bits in hex at least)
105- if (token .length ()>=12 )
106- {
107- // lookup the authorization -- we must have one to correspond to an access token
108- try {
109- authorizationFromToken = authorizationService .findByAccessToken (token );
110-
111- }
112- catch (Exception e ) {
113- System .out .printf ("ResourceValidationFilter: doFilter - No Authorization Found - %s\n " ,
114- e .toString ());
115- throw new AccessDeniedException (String .format ("No Authorization Found" ));
116- }
101+ // has Authorization header with Bearer type
102+ hasBearer = true ;
103+ token = token .replace ("Bearer " , "" );
117104
118- // see if we have valid authorization and can get parameters
119- if (authorizationFromToken != null )
120- {
121- long tend = authorizationFromToken . getAuthorizedPeriod (). getStart () + authorizationFromToken . getAuthorizedPeriod (). getDuration ();
122- Calendar c = Calendar . getInstance ();
123- long now = c . getTimeInMillis ()/ 1000 ;
105+ // ensure length is >12 characters (48 bits in hex at least)
106+ if (token . length ()>= 12 )
107+ {
108+ // lookup the authorization -- we must have one to correspond to an access token
109+ try {
110+ authorizationFromToken = authorizationService . findByAccessToken ( token ) ;
124111
125- // check that it is still active and check that it has not expired
112+ hasValidOAuthAccessToken = true ;
113+ resourceUri = authorizationFromToken .getResourceURI ();
114+ authorizationUri = authorizationFromToken .getAuthorizationURI ();
115+ subscription = authorizationFromToken .getSubscription ();
126116
127- if ( (authorizationFromToken .getStatus ().equals ("1" ) ) && ((tend == 0 ) || (tend >= now ))){
128- hasValidOAuthAccessToken = true ;
129- resourceUri = authorizationFromToken .getResourceURI ();
130- authorizationUri = authorizationFromToken .getAuthorizationURI ();
131- subscription = authorizationFromToken .getSubscription ();
132-
133- } else {
134-
135- // authorization not valid now
136- System .out .printf ("ResourceValidationFilter: doFilter - Access Not Authorized\n " );
137- throw new AccessDeniedException (String .format ("Access Not Authorized" ));
138- }
139- }
117+ }
118+ catch (Exception e ) {
119+ System .out .printf ("ResourceValidationFilter: doFilter - No Authorization Found - %s\n " ,
120+ e .toString ());
121+ throw new AccessDeniedException (String .format ("No Authorization Found" ));
140122 }
141123 }
142124 }
@@ -400,32 +382,23 @@ else if (invalid && roles.contains("ROLE_TP_REGISTRATION")) {
400382 if (applicationInformationIdFromUri .equals (applicationInformationId )) {
401383 invalid = false ;
402384 }
403- }
404- else
405- {
385+
386+ } else {
406387 // not authorized for this resource
407388 System .out .printf ("ResourceValidationFilter: doFilter - Access Not Authorized\n " );
408389 throw new AccessDeniedException (String .format ("Access Not Authorized" ));
409390 }
410- }
411- }
412- else
413- {
414- // not authorized for this resource
415- System .out .printf ("ResourceValidationFilter: doFilter - Access Not Authorized\n " );
416- throw new AccessDeniedException (String .format ("Access Not Authorized" ));
417- }
418-
419- // check if it is this authorization request
420- if (uri .contains ("/resource/Authorization" )) {
421- if (authorizationUri .equals (uri ) && service .equals ("GET" )) {
422- invalid =false ;
423- }
424- else {
391+
392+ } else {
425393 // not authorized for this resource
426394 System .out .printf ("ResourceValidationFilter: doFilter - Access Not Authorized\n " );
427395 throw new AccessDeniedException (String .format ("Access Not Authorized" ));
428396 }
397+
398+ } else {
399+ // not authorized for this resource
400+ System .out .printf ("ResourceValidationFilter: doFilter - Access Not Authorized\n " );
401+ throw new AccessDeniedException (String .format ("Access Not Authorized" ));
429402 }
430403 }
431404 }
0 commit comments