<commit_before>package org.railwaystations.api.resources;

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.dropwizard.auth.Auth;
import org.eclipse.jetty.http.HttpStatus;
import org.railwaystations.api.TokenGenerator;
import org.railwaystations.api.auth.AuthUser;
import org.railwaystations.api.db.UserDao;
import org.railwaystations.api.mail.Mailer;
import org.railwaystations.api.model.User;
import org.railwaystations.api.monitoring.Monitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Optional;

@Path("/")
public class ProfileResource {

    private static final Logger LOG = LoggerFactory.getLogger(ProfileResource.class);
    private static final JacksonFactory JACKSON_FACTORY = new JacksonFactory();

    private final TokenGenerator tokenGenerator;
    private final Monitor monitor;
    private final Mailer mailer;
    private final UserDao userDao;
    private final String googleClientId;

    public ProfileResource(final TokenGenerator tokenGenerator, final Monitor monitor, final Mailer mailer, final UserDao userDao, final String googleClientId) {
        this.tokenGenerator = tokenGenerator;
        this.monitor = monitor;
        this.mailer = mailer;
        this.userDao = userDao;
        this.googleClientId = googleClientId;
    }

    @POST
    @Path("registration")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response register(@NotNull final User registration) {
        LOG.info("New registration for '{}' with '{}'", registration.getName(), registration.getEmail());

        if (!registration.isValidForRegistration()) {
            LOG.warn("Registration for '{}' with '{}' invalid", registration.getName(), registration.getEmail());
            return Response.status(Response.Status.BAD_REQUEST).build();
        }

        return register(registration, false);
    }

    private void createNewUploadToken(@NotNull final User user) {
        user.setUploadTokenSalt(System.currentTimeMillis());
        user.setUploadToken(tokenGenerator.buildFor(user.getEmail(), user.getUploadTokenSalt()));
    }

    @POST
    @Path("registration/withGoogleIdToken")
    @Produces(MediaType.APPLICATION_JSON)
    public Response registerWithGoogleIdToken(@HeaderParam("Google-Id-Token") final String idToken) {
        LOG.info("New GoogleIdToken registration");

        final GoogleIdToken.Payload googleLogin = verifyGoogleIdToken(idToken);
        if (googleLogin == null) {
            return Response.status(Response.Status.UNAUTHORIZED).build();
        }

        return register(new User(googleLogin.get("name").toString(), googleLogin.getEmail(), null, false, null, false), googleLogin.getEmailVerified());
    }

    private Response register(final User registration, final boolean eMailVerified) {
        final Optional<User> existingName = userDao.findByNormalizedName(registration.getNormalizedName());
        if (existingName.isPresent() && !registration.getEmail().equals(existingName.get().getEmail())) {
            monitor.sendMessage(
                    String.format("Registration for user '%s' with eMail '%s' failed, because name is already taken by different eMail '%s'",
                            registration.getName(), registration.getEmail(), existingName.get().getEmail()));<commit_msg>complete google registration<commit_after>package org.railwaystations.api.resources;

import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.dropwizard.auth.Auth;
import org.eclipse.jetty.http.HttpStatus;
import org.railwaystations.api.TokenGenerator;
import org.railwaystations.api.auth.AuthUser;
import org.railwaystations.api.db.UserDao;
import org.railwaystations.api.mail.Mailer;
import org.railwaystations.api.model.User;
import org.railwaystations.api.monitoring.Monitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.validation.constraints.NotNull;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Optional;

@Path("/")
public class ProfileResource {

    private static final Logger LOG = LoggerFactory.getLogger(ProfileResource.class);
    private static final JacksonFactory JACKSON_FACTORY = new JacksonFactory();

    private final TokenGenerator tokenGenerator;
    private final Monitor monitor;
    private final Mailer mailer;
    private final UserDao userDao;
    private final String googleClientId;

    public ProfileResource(final TokenGenerator tokenGenerator, final Monitor monitor, final Mailer mailer, final UserDao userDao, final String googleClientId) {
        this.tokenGenerator = tokenGenerator;
        this.monitor = monitor;
        this.mailer = mailer;
        this.userDao = userDao;
        this.googleClientId = googleClientId;
    }

    @POST
    @Path("registration")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response register(@NotNull final User registration) {
        LOG.info("New registration for '{}' with '{}'", registration.getName(), registration.getEmail());

        if (!registration.isValidForRegistration()) {
            LOG.warn("Registration for '{}' with '{}' invalid", registration.getName(), registration.getEmail());
            return Response.status(Response.Status.BAD_REQUEST).build();
        }

        return register(registration, false);
    }

    private void createNewUploadToken(@NotNull final User user) {
        user.setUploadTokenSalt(System.currentTimeMillis());
        user.setUploadToken(tokenGenerator.buildFor(user.getEmail(), user.getUploadTokenSalt()));
    }

    @POST
    @Path("registration/withGoogleIdToken")
    @Produces(MediaType.APPLICATION_JSON)
    public Response registerWithGoogleIdToken(@HeaderParam("Google-Id-Token") final String idToken, @NotNull final User registration) {
        LOG.info("New GoogleIdToken registration");

        final GoogleIdToken.Payload googleLogin = verifyGoogleIdToken(idToken);
        if (googleLogin == null) {
            return Response.status(Response.Status.UNAUTHORIZED).build();
        }

        return register(new User(googleLogin.get("name").toString(), googleLogin.getEmail(), registration.getLicense(),
                registration.isOwnPhotos(), registration.getUrl(), registration.isAnonymous()), googleLogin.getEmailVerified());
    }

    private Response register(final User registration, final boolean eMailVerified) {
        final Optional<User> existingName = userDao.findByNormalizedName(registration.getNormalizedName());
        if (existingName.isPresent() && !registration.getEmail().equals(existingName.get().getEmail())) {
            monitor.sendMessage(
                    String.format("Registration for user '%s' with eMail '%s' failed, because name is already taken by different eMail '%s'",
                            registration.getName(), registration.getEmail(), existingName.get().getEmail()));
{'fraction_non_alphanumeric': 0.10851794811041346, 'fraction_numerical': 0.0011846937566639024, 'mean_word_length': 4.332912192040429, 'pattern_counts': {'":': 0, '<': 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': '5273267', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2315, 'n_words': 471}