Skip to content
This repository has been archived by the owner on Mar 26, 2018. It is now read-only.

Commit

Permalink
feat(client side state): Implements client side...
Browse files Browse the repository at this point in the history
... state storage

OAuth1 and OAuth2 implementations used different state storage, this
changes that to use the same storage, which has been implemented on the
client side as a cookie.

For this the flow state had to be split into two variants one for OAuth1
and one for OAuth2, and state access had to be revised thought the
implementation.
  • Loading branch information
zregvart committed Jul 26, 2017
1 parent 3c9984d commit d196958
Show file tree
Hide file tree
Showing 22 changed files with 625 additions and 279 deletions.
11 changes: 0 additions & 11 deletions credential/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@
<artifactId>validation-api</artifactId>
</dependency>

<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
</dependency>

<!-- === Supported services ============================================================== -->

<dependency>
Expand Down Expand Up @@ -167,12 +162,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@
*/
package io.syndesis.credential;

import java.util.Optional;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import org.immutables.value.Value;

@Value.Immutable
@JsonDeserialize(builder = Acquisition.Builder.class)
public interface Acquisition {

class Builder extends ImmutableAcquisition.Builder {
@JsonDeserialize(builder = AcquisitionFlow.Builder.class)
public interface AcquisitionFlow {

class Builder extends ImmutableAcquisitionFlow.Builder {
// builder implemented by Immutables, access allowed through this
// subclass
}

enum Type {
REDIRECT
}
String getRedirectUrl();

Type getType();

String getUrl();
Optional<CredentialFlowState> state();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ class Builder extends ImmutableAcquisitionMethod.Builder {
// subclass
}

enum Type {
OAUTH1, OAUTH2
}

String getDescription();

String getIcon();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (C) 2016 Red Hat, Inc.
*
* 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 io.syndesis.credential;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import org.immutables.value.Value;

@Value.Immutable
@JsonDeserialize(builder = AcquisitionResponse.Builder.class)
public interface AcquisitionResponse {

class Builder extends ImmutableAcquisitionResponse.Builder {

public static AcquisitionResponse.Builder from(final AcquisitionFlow flow) {
return new AcquisitionResponse.Builder().type(flow.getType()).redirectUrl(flow.getRedirectUrl());
}

}

@Value.Immutable
@JsonDeserialize(builder = State.Builder.class)
interface State {

class Builder extends ImmutableState.Builder {

public static State cookie(final String spec) {
return new State.Builder().spec(spec).persist(Persist.COOKIE).build();
}

}

enum Persist {
COOKIE
}

Persist persist();

String spec();
}

String getRedirectUrl();

Type getType();

State state();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import io.syndesis.dao.manager.DataManager;

import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -31,7 +30,7 @@ public CredentialProviderLocator credentialProviderLocator() {

@Bean
public Credentials credentials(final CredentialProviderLocator connectionProviderLocator,
final DataManager dataManager, final CacheManager cacheManager) {
return new Credentials(connectionProviderLocator, dataManager, cacheManager);
final DataManager dataManager) {
return new Credentials(connectionProviderLocator, dataManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,94 @@

import java.net.URI;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import javax.servlet.http.HttpServletRequest;

import org.immutables.value.Value;
import org.springframework.social.oauth1.OAuthToken;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@Value.Immutable
@JsonDeserialize(builder = CredentialFlowState.Builder.class)
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.support.OAuth1ConnectionFactory;
import org.springframework.social.connect.support.OAuth2ConnectionFactory;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = ImmutableOAuth1CredentialFlowState.class,
name = "OAUTH1"),
@com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = ImmutableOAuth2CredentialFlowState.class,
name = "OAUTH2")})
public interface CredentialFlowState {

class Builder extends ImmutableCredentialFlowState.Builder {
// builder implemented by Immutables, access allowed through this
// subclass
String CREDENTIAL_PREFIX = "cred-o";

String OAUTH1_CREDENTIAL_PREFIX = CREDENTIAL_PREFIX + "1-";

String OAUTH2_CREDENTIAL_PREFIX = CREDENTIAL_PREFIX + "2-";

interface Builder {

CredentialFlowState build();

Builder connectionId(String connectionId);

Builder key(String key);

Builder providerId(String providerId);

Builder returnUrl(URI returnUrl);

}

String getConnectionId();

String getKey();

String getProviderId();

URI getReturnUrl();

String getKey();
default String persistenceKey() {
return statePrefix() + getKey();
}

default String statePrefix() {
if (this instanceof OAuth1CredentialFlowState) {
return OAUTH1_CREDENTIAL_PREFIX;
} else if (this instanceof OAuth2CredentialFlowState) {
return OAUTH2_CREDENTIAL_PREFIX;
}

throw new IllegalStateException("Unsupported credential flow state implementation: " + this);
}

default Type type() {
if (this instanceof OAuth1CredentialFlowState) {
return Type.OAUTH1;
} else if (this instanceof OAuth2CredentialFlowState) {
return Type.OAUTH2;
}

throw new IllegalStateException("Unsupported credential flow state implementation: " + this);
}

OAuthToken getToken();
CredentialFlowState updateFrom(HttpServletRequest request);

static Builder builderFor(final ConnectionFactory<?> connectionFactory) {
if (connectionFactory instanceof OAuth1ConnectionFactory) {
return new OAuth1CredentialFlowState.Builder();
} else if (connectionFactory instanceof OAuth2ConnectionFactory) {
return new OAuth2CredentialFlowState.Builder();
}

throw new IllegalStateException("Unsupported connection factory implementation: " + connectionFactory);
}

static Class<? extends CredentialFlowState> typeForName(final String name) {
if (name.startsWith(OAUTH1_CREDENTIAL_PREFIX)) {
return OAuth1CredentialFlowState.class;
} else if (name.startsWith(OAUTH2_CREDENTIAL_PREFIX)) {
return OAuth2CredentialFlowState.class;
}

throw new IllegalStateException("Unsupported credential flow name: " + name);
}
}
Loading

0 comments on commit d196958

Please sign in to comment.