// Path: src/main/java/org/dmfs/oauth2/client/utils/Parameters.java
// public final static ParameterType<CharSequence> AUTH_CODE = new BasicParameterType<>("code", TextValueType.INSTANCE);
// 
// Path: src/main/java/org/dmfs/oauth2/client/utils/Parameters.java
// public final static ParameterType<CharSequence> STATE = new BasicParameterType<>("state", TextValueType.INSTANCE);


import org.dmfs.httpessentials.exceptions.ProtocolException;
import org.dmfs.rfc3986.Uri;
import org.dmfs.rfc3986.parameters.ParameterList;
import org.dmfs.rfc3986.parameters.adapters.OptionalParameter;
import org.dmfs.rfc3986.parameters.adapters.TextParameter;
import org.dmfs.rfc3986.parameters.adapters.XwfueParameterList;
import static org.dmfs.oauth2.client.utils.Parameters.AUTH_CODE;
import static org.dmfs.oauth2.client.utils.Parameters.STATE;
/*
 * Copyright 2016 dmfs GmbH
 *
 * 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.dmfs.oauth2.client;




/**
 * A basic {@link OAuth2AuthCodeAuthorization} implementation.
 * <p>
 * Note: Usually you don't need to instantiate this directly.
 *
 * @author Marten Gajda
 */
public final class BasicOAuth2AuthCodeAuthorization implements OAuth2AuthCodeAuthorization
{
    private final ParameterList mQueryParameters;
    private final OAuth2Scope mScope;


    public BasicOAuth2AuthCodeAuthorization(Uri redirectUri, OAuth2Scope requestedScope, CharSequence state) throws ProtocolException
    {
        mQueryParameters = new XwfueParameterList(redirectUri.query().value());
        if (!state.toString().equals(new TextParameter(STATE, mQueryParameters).toString()))
        {
            throw new ProtocolException("State in redirect uri doesn't match the original state!");
        }
// Path: src/main/java/org/dmfs/oauth2/client/utils/Parameters.java
// public final static ParameterType<CharSequence> AUTH_CODE = new BasicParameterType<>("code", TextValueType.INSTANCE);
// 
// Path: src/main/java/org/dmfs/oauth2/client/utils/Parameters.java
// public final static ParameterType<CharSequence> STATE = new BasicParameterType<>("state", TextValueType.INSTANCE);


// Path: src/main/java/org/dmfs/oauth2/client/BasicOAuth2AuthCodeAuthorization.java
import org.dmfs.httpessentials.exceptions.ProtocolException;
import org.dmfs.rfc3986.Uri;
import org.dmfs.rfc3986.parameters.ParameterList;
import org.dmfs.rfc3986.parameters.adapters.OptionalParameter;
import org.dmfs.rfc3986.parameters.adapters.TextParameter;
import org.dmfs.rfc3986.parameters.adapters.XwfueParameterList;
import static org.dmfs.oauth2.client.utils.Parameters.AUTH_CODE;
import static org.dmfs.oauth2.client.utils.Parameters.STATE;

/*
 * Copyright 2016 dmfs GmbH
 *
 * 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.dmfs.oauth2.client;




/**
 * A basic {@link OAuth2AuthCodeAuthorization} implementation.
 * <p>
 * Note: Usually you don't need to instantiate this directly.
 *
 * @author Marten Gajda
 */
public final class BasicOAuth2AuthCodeAuthorization implements OAuth2AuthCodeAuthorization
{
    private final ParameterList mQueryParameters;
    private final OAuth2Scope mScope;


    public BasicOAuth2AuthCodeAuthorization(Uri redirectUri, OAuth2Scope requestedScope, CharSequence state) throws ProtocolException
    {
        mQueryParameters = new XwfueParameterList(redirectUri.query().value());
        if (!state.toString().equals(new TextParameter(STATE, mQueryParameters).toString()))
        {
            throw new ProtocolException("State in redirect uri doesn't match the original state!");
        }
        if (!new OptionalParameter<CharSequence>(AUTH_CODE, mQueryParameters).isPresent())