package com.atlassian.stash.internal.auth.trusted;

import com.atlassian.bitbucket.license.LicenseService;
import com.atlassian.bitbucket.auth.AuthenticationContext;
import com.atlassian.security.auth.trustedapps.filter.AuthenticationController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Component
public class TrustedApplicationsAuthController implements AuthenticationController {

    private final AuthenticationContext authenticationContext;
    private final LicenseService licenseService;

    @Autowired
    public TrustedApplicationsAuthController(AuthenticationContext authenticationContext, LicenseService licenseService) {
        this.authenticationContext = authenticationContext;
        this.licenseService = licenseService;
    }

    @Override
    public boolean shouldAttemptAuthentication(HttpServletRequest request) {
        return !authenticationContext.isAuthenticated();
    }

    @Override
    public boolean canLogin(Principal principal, HttpServletRequest request) {
        return licenseService.canLogin(principal);
    }
}
