Determine if our controlled objects should be authorized based on the
provided authentication token.

@param authentication
token
@return true if should authorize
326174
protected boolean shouldAuthorize(Authentication authentication, Authorizable controlledObject)
    {
        Assert.state(getAccessDecisionManager() != null, "The AccessDecisionManager can not be null!");
        boolean authorize = false;
        try
        {
            if (authentication != null)
            {
                List<ConfigAttribute> cad = getConfigAttributeDefinition(controlledObject);
                if (cad != null)
                {
                    getAccessDecisionManager().decide(authentication, null, cad);
                }
                authorize = true;
            } else {
                // authentication must be disabled, going through
                authorize = true;
            }
        }
        catch (AccessDeniedException e)
        {
            authorize = false;
            // This means the secured objects should not be authorized
        }
        return authorize;
    }
1210001
protected boolean requestRequiresAuthentication(final HttpServletRequest request, final HttpServletResponse response) {
        val accessTokenRequest = isAccessTokenRequest(request, response);
        if (!accessTokenRequest) {
            val extractor = extractAccessTokenGrantRequest(request);
            if (extractor.isPresent()) {
                val ext = extractor.get();
                return ext.requestMustBeAuthenticated();
            }
        } else {
            val extractor = extractAccessTokenGrantRequest(request);
            if (extractor.isPresent()) {
                val ext = extractor.get();
                return ext.getResponseType() != OAuth20ResponseTypes.DEVICE_CODE;
            }
        }
        return false;
    }
1068034
public void setoAuthAccessToken(OAuthAccessToken oAuthAccessToken) {
    this.oAuthAccessToken = oAuthAccessToken;
    this.accessToken = new Token(oAuthAccessToken.getOauthToken(), oAuthAccessToken.getOauthTokenSecret());
  }
168124
public void setEnableAuthentication( boolean enableAuthentication ) {

		this.logger.fine( "Authentication is now " + (enableAuthentication ? "enabled" : "disabled") + ". Updating the REST resource." );
		this.enableAuthentication = enableAuthentication;

		if( this.authenticationFilter != null )
			this.authenticationFilter.setAuthenticationEnabled( enableAuthentication );
	}
996487
protected void updateControlledObject(Authorizable controlledObject, boolean authorized) {
        if( logger.isDebugEnabled() ) {
            logger.debug( "setAuthorized( " + authorized + ") on: " + controlledObject );
        }
        controlledObject.setAuthorized( authorized );
        runPostProcessorActions( controlledObject, authorized );
    }
161498
public static boolean isAuthorizedResponseTypeForService(final J2EContext context, final OAuthRegisteredService registeredService) {
        val responseType = context.getRequestParameter(OAuth20Constants.RESPONSE_TYPE);
        if (registeredService.getSupportedResponseTypes() != null && !registeredService.getSupportedResponseTypes().isEmpty()) {
            LOGGER.debug("Checking response type [{}] against supported response types [{}]", responseType, registeredService.getSupportedResponseTypes());
            return registeredService.getSupportedResponseTypes().stream().anyMatch(s -> s.equalsIgnoreCase(responseType));
        }

        LOGGER.warn("Registered service [{}] does not define any authorized/supported response types. "
            + "It is STRONGLY recommended that you authorize and assign response types to the service definition. "
            + "While just a warning for now, this behavior will be enforced by CAS in future versions.", registeredService.getName());
        return true;
    }
1154974
public boolean authenticate(HttpServletRequest request) throws MnoException {
		String authHeader = request.getHeader("Authorization");
		if (authHeader == null || authHeader.isEmpty()) {
			return false;
		}
		String[] auth = authHeader.trim().split("\\s+");

		if (auth == null || auth.length != 2 || !auth[0].equalsIgnoreCase("basic")) {
			return false;
		}
		byte[] decodedStr = DatatypeConverter.parseBase64Binary(auth[1]);
		String[] creds;
		try {
			creds = (new String(decodedStr, "UTF-8")).split(":");
		} catch (UnsupportedEncodingException e) {
			throw new MnoException("Could not decode basic authentication" + Arrays.toString(auth), e);
		}

		if (creds.length == 2) {
			return authenticate(creds[0], creds[1]);
		} else {
			return false;
		}
	}
703130
@SuppressWarnings("unchecked")
    public boolean hasPermission(Authentication authentication,
                                 Serializable resource,
                                 String resourceType,
                                 Object privilege) {
        boolean logPermission = isLogPermission(resource);
        if (checkRole(authentication, privilege, logPermission)) {
            return true;
        }
        if (resource != null) {
            Object resourceId = ((Map<String, Object>) resource).get("id");
            if (resourceId != null) {
                ((Map<String, Object>) resource).put(resourceType,
                    resourceFactory.getResource(resourceId, resourceType));
            }
        }
        return checkPermission(authentication, resource, privilege, true, logPermission);
    }
1243146
private boolean hasAuthenticatedMolgenisToken() {
    boolean hasAuthenticatedMolgenisToken = false;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication instanceof RestAuthenticationToken) {
      hasAuthenticatedMolgenisToken = authentication.isAuthenticated();
    }
    return hasAuthenticatedMolgenisToken;
  }
407779
public boolean hasValidCredentials() {
        String accessToken = storage.retrieveString(KEY_ACCESS_TOKEN);
        String refreshToken = storage.retrieveString(KEY_REFRESH_TOKEN);
        String idToken = storage.retrieveString(KEY_ID_TOKEN);
        Long expiresAt = storage.retrieveLong(KEY_EXPIRES_AT);

        return !(isEmpty(accessToken) && isEmpty(idToken) ||
                expiresAt == null ||
                expiresAt <= getCurrentTimeInMillis() && refreshToken == null);
    }
522506
private boolean isAuthorizationRequired(Response response) {
        if (response != null && response.getStatus() == 401) {
            ResponseImpl responseImpl = (ResponseImpl)response;
            String challengesHeader = responseImpl.getFirstHeader(AUTHENTICATE_HEADER_NAME);

            if (AUTHENTICATE_HEADER_VALUE.equalsIgnoreCase(challengesHeader)) {
                return true;
            }
        }

        return false;
    }
1043007
@Override
  public boolean canAuthenticate(String resource) {
    if (resource == null) {
      return false;
    }

    ClientCredential credential = getCredentialForResource(resource);
    return credential != null;
  }
600152
public Authentication getAuthentication(String token) {
		if (null != token) {
			TokenContainer container = tokens.get(token);
			if (null != container) {
				if (container.isValid()) {
					return container.getAuthentication();
				} else {
					logout(token);
				}
			}
		}
		return null;
	}
576217
private boolean isAuthoriedAccess(MethodDescriptor<String> desc, String appId, String token) {
        if (CollectionUtil.isNotEmpty(desc.getAppIdTokens())) {
            for (AppIdToken appIdToken : desc.getAppIdTokens()) {
                if (AppIdToken.isValid(appIdToken, appId, token)) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }
824228
def authorized?(action, subject)
      return false unless subject
      klass = subject.is_a?(Class) ? subject : subject.class
      authorizer_of(klass).authorized? action, subject
    end
977155
public void setAuthorized( boolean authorized ) {

        if( isAuthorized() != authorized ) {

            this.authorized = authorized;

            // We need to apply a change to our enabled state depending on our
            // new authorized state.
            if( authorized ) {
                // install the last requested enabled state
                setEnabled(maskedEnabledState);
            } else {
                // Record the current enabled state and then disable
                maskedEnabledState = isEnabled();
                internalSetEnabled(false);
            }
        }
    }
396471
public static boolean currentUserIsAuthenticated() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return authentication != null && authentication.isAuthenticated() && !currentUserIsAnonymous();
  }