<commit_before>/*
  Copyright 2012 -2014 pac4j organization

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package org.pac4j.oidc.profile;

import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTParser;
import com.nimbusds.oauth2.sdk.Scope;
import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.oidc.client.OidcClient;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.text.ParseException;
import java.util.List;

/**
 * <p>This class is the user profile for sites using OpenID Connect protocol.</p>
 * <p>It is returned by the {@link OidcClient}.</p>
 * 
 * @author <NAME>
 * @version 1.7.0
 */
public class OidcProfile extends CommonProfile implements Externalizable {

    private static final long serialVersionUID = -52855988661742374L;

    private BearerAccessToken accessToken;
    private String idTokenString;
    private JWT idToken;

    public OidcProfile() {
    }

    public OidcProfile(BearerAccessToken accessToken) {
        this.accessToken = accessToken;
    }

    public BearerAccessToken getAccessToken() {
        return this.accessToken;
    }

    public String getIdTokenString() {
        return idTokenString;
    }

    public void setIdTokenString(String idTokenString) {
        this.idTokenString = idTokenString;
    }

    public JWT getIdToken() throws ParseException {
        if (this.idToken == null && this.idTokenString != null) {
            this.idToken = JWTParser.parse(this.idTokenString);
        }

        return this.idToken;
    }


    @Override
    public void writeExternal(final ObjectOutput out) throws IOException {
        super.writeExternal(out);
        final BearerAccessTokenBean bean = BearerAccessTokenBean.toBean(this.accessToken);
        out.writeObject(bean);
    }

    @Override
    public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
        super.readExternal(in);
        final BearerAccessTokenBean bean = (BearerAccessTokenBean) in.readObject();
        this.accessToken = BearerAccessTokenBean.fromBean(bean);
    }

    private static class BearerAccessTokenBean implements Serializable {
        private String value;
        private long lifetime;
        private List<String> scope;

        public BearerAccessTokenBean(final String value, final long lifetime,
                                     final List<String> scope) {
            this.value = value;
            this.lifetime = lifetime;
            this.scope = scope;
        }

        public static BearerAccessTokenBean toBean(final BearerAccessToken token) {<commit_msg>`idTokenString` will now serialize as expected.<commit_after>/*
  Copyright 2012 -2014 pac4j organization

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package org.pac4j.oidc.profile;

import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTParser;
import com.nimbusds.oauth2.sdk.Scope;
import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.oidc.client.OidcClient;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.text.ParseException;
import java.util.List;

/**
 * <p>This class is the user profile for sites using OpenID Connect protocol.</p>
 * <p>It is returned by the {@link OidcClient}.</p>
 *
 * @author <NAME>
 * @version 1.7.0
 */
public class OidcProfile extends CommonProfile implements Externalizable {

    private static final long serialVersionUID = -52855988661742374L;

    private BearerAccessToken accessToken;
    private String idTokenString;
    private JWT idToken;

    public OidcProfile() {
    }

    public OidcProfile(BearerAccessToken accessToken) {
        this.accessToken = accessToken;
    }

    public BearerAccessToken getAccessToken() {
        return this.accessToken;
    }

    public String getIdTokenString() {
        return idTokenString;
    }

    public void setIdTokenString(String idTokenString) {
        this.idTokenString = idTokenString;
    }

    public JWT getIdToken() throws ParseException {
        if (this.idToken == null && this.idTokenString != null) {
            this.idToken = JWTParser.parse(this.idTokenString);
        }

        return this.idToken;
    }


    @Override
    public void writeExternal(final ObjectOutput out) throws IOException {
        super.writeExternal(out);
        final BearerAccessTokenBean bean = BearerAccessTokenBean.toBean(this.accessToken);
        out.writeObject(bean);
        out.writeObject(this.idTokenString);
    }

    @Override
    public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
        super.readExternal(in);
        final BearerAccessTokenBean bean = (BearerAccessTokenBean) in.readObject();
        this.accessToken = BearerAccessTokenBean.fromBean(bean);
        this.idTokenString = (String) in.readObject();
    }

    private static class BearerAccessTokenBean implements Serializable {
        private String value;
        private long lifetime;
        private List<String> scope;

        public BearerAccessTokenBean(final String value, final long lifetime,
                                     final List<String> scope) {
            this.value = value;
            this.lifetime = lifetime;
            this.scope = scope;
        }

        public static BearerAccessTokenBean toBean(final BearerAccessToken token) {
{'fraction_non_alphanumeric': 0.0768183880235899, 'fraction_numerical': 0.011492514743686677, 'mean_word_length': 3.1519146264908975, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7249289', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1806, 'n_words': 533}