<commit_before>package dashboard.control;

import dashboard.domain.Action;
import dashboard.domain.InviteRequest;
import dashboard.domain.Service;
import dashboard.mail.MailBox;
import dashboard.manage.EntityType;
import dashboard.sab.Sab;
import dashboard.sab.SabPerson;
import dashboard.service.ActionsService;
import dashboard.service.Services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


@RequestMapping(value = "/serviceProvider/api/")
@RestController
public class ServiceProviderController extends BaseController {
    private static final Logger LOG = LoggerFactory.getLogger(ServiceProviderController.class);

    @Autowired
    private Services services;

    @Autowired
    private ActionsService actionsService;

    @Autowired
    private MailBox mailbox;

    @Autowired
    private Sab sabClient;

    // authenticate using shib headers before calling this function
    @RequestMapping(value = "serviceConnectionRequest", method = RequestMethod.PUT)
    public void connectionRequest(
            @RequestBody InviteRequest inviteRequest,
            @RequestParam Locale locale,
            @RequestParam String contactName,
            @RequestParam String contactEmail,
            @RequestParam String ownEmail
    ) throws IOException, MessagingException {
        LOG.debug("Incoming connection request, params(" +
                "IdpEntityId: " + inviteRequest.getIdpEntityId() +
                " SpEntityId: " + inviteRequest.getSpEntityId() +
                " contactName " + contactName +
                " contactEmail " + contactEmail +
                " ownEmail " + ownEmail + ")");

        String idpEntityId = inviteRequest.getIdpEntityId();
        String spEntityId = inviteRequest.getSpEntityId();

        Optional<Service> service = services.getServiceByEntityId(idpEntityId, spEntityId, EntityType.saml20_sp, locale);

        if (service.isPresent()) { // check if service is already connected. If it is, ignore the request?
            return;
        }

        String emailTo = sabClient.getPersonsInRoleForOrganization(idpEntityId, "SURFconextverantwoordelijke")
                .stream()<commit_msg>basic set up of connectionRequest needs to be redone when I can authenticate<commit_after>package dashboard.control;

import dashboard.domain.Action;
import dashboard.domain.InviteRequest;
import dashboard.domain.Service;
import dashboard.mail.MailBox;
import dashboard.manage.EntityType;
import dashboard.sab.Sab;
import dashboard.sab.SabPerson;
import dashboard.service.ActionsService;
import dashboard.service.Services;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;


@RequestMapping(value = "/serviceProvider/api/")
@RestController
public class ServiceProviderController extends BaseController {
    private static final Logger LOG = LoggerFactory.getLogger(ServiceProviderController.class);

    @Autowired
    private Services services;

    @Autowired
    private ActionsService actionsService;

    @Autowired
    private MailBox mailbox;

    @Autowired
    private Sab sabClient;

    // TODO: rewrite this
    @RequestMapping(value = "serviceConnectionRequest", method = RequestMethod.PUT)
    public void connectionRequest(
            @RequestParam InviteRequest inviteRequest,
            @RequestParam String contactName,
            @RequestParam String contactEmail,
            @RequestParam String ownEmail,
            HttpServletRequest request
    ) throws IOException, MessagingException {
        LOG.debug("authenticating serviceProvider request from getSpEntityId: " + inviteRequest.getSpEntityId());

        String header = request.getHeader("Authorization");
        if (header == null || !header.startsWith("Basic ")) {
            return;
        }

        byte[] base64Token = header.substring(6).getBytes(Charset.defaultCharset());
        byte[] decoded;
        try {
            decoded = Base64.getDecoder().decode(base64Token);
        } catch (IllegalArgumentException e) {
            return;
        }


        String token = new String(decoded, Charset.defaultCharset());
        try {
            String user = token.split(":")[0];
            String password = token.split(":")[1];
            if (user.equals("") || password.equals("")) {
                return;
            }
        } catch (Exception e) {
            LOG.debug(String.valueOf(e));
        }

        // authentication done

        LOG.debug("Incoming connection request, params(" +
                "IdpEntityId: " + inviteRequest.getIdpEntityId() +
                " SpEntityId: " + inviteRequest.getSpEntityId() +
                " contactName " + contactName +
                " contactEmail " + contactEmail +
                " ownEmail " + ownEmail + ")");

        String idpEntityId = inviteRequest.getIdpEntityId();
        String spEntityId = inviteRequest.getSpEntityId();

        Locale locale = this.getLocale(request);
        Optional<Service> service = services.getServiceByEntityId(idpEntityId, spEntityId, EntityType.saml20_sp, locale);

        if (service.isPresent()) { // check if service is already connected. If it is, ignore the request?
            return;
        }

        String emailTo = sabClient.getPersonsInRoleForOrganization(idpEntityId, "SURFconextverantwoordelijke")
                .stream()
{'fraction_non_alphanumeric': 0.09081615266655307, 'fraction_numerical': 0.002896575225762481, 'mean_word_length': 3.148409893992933, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6746776', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1494, 'n_words': 350}