Skip to content

Commit

Permalink
[java][social_login-01_base] Add base structure
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas committed Apr 26, 2021
1 parent 94f3d6f commit cb77b9f
Show file tree
Hide file tree
Showing 26 changed files with 206 additions and 47 deletions.
4 changes: 2 additions & 2 deletions examples/java/java-social_login-01_base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ dependencies {
implementation 'org.apache.logging.log4j:log4j-core:2.12.1'
implementation 'com.vlkan.log4j2:log4j2-logstash-layout:0.19'

testCompile 'org.junit.jupiter:junit-jupiter-api:5.5.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
}

test {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tv.codely.ecommerce.login;
package tv.codely.ecommerce.login.domain;

public final class EmailLoginProvider extends LoginProvider {
private final String email;
private final String password;

public EmailLoginProvider(String email, String password, String ip) {
super("email", ip);

this.email = email;
this.password = password;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tv.codely.ecommerce.login;
package tv.codely.ecommerce.login.domain;

public final class FacebookLoginProvider extends LoginProvider {
private final String email;
private final String hash;

public FacebookLoginProvider(String email, String hash, String ip) {
super("facebook", ip);

this.email = email;
this.hash = hash;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tv.codely.ecommerce.login;
package tv.codely.ecommerce.login.domain;

public final class GitHubLoginProvider extends LoginProvider {
private final String username;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tv.codely.ecommerce.login;
package tv.codely.ecommerce.login.domain;

public abstract class LoginProvider {
private String provider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tv.codely.ecommerce.login;
package tv.codely.ecommerce.login.domain;

public final class TwitterLoginProvider extends LoginProvider {
private String username;
private String token;

public TwitterLoginProvider(String username, String token, String ip) {
super("twitter", ip);

this.username = username;
this.token = token;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tv.codely.ecommerce.login_attempt.application.add;

import tv.codely.ecommerce.login_attempt.domain.LoginAttempt;
import tv.codely.ecommerce.login_attempt.domain.LoginAttemptRepository;

public final class LoginAttemptAdder {
private final LoginAttemptRepository repository;

public LoginAttemptAdder(LoginAttemptRepository repository) {
this.repository = repository;
}

public void add(LoginAttempt loginAttempt) throws Exception {
this.repository.save(loginAttempt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tv.codely.ecommerce.login_attempt.domain;

public final class EmailLoginAttempt extends LoginAttempt {
private final String email;

public EmailLoginAttempt(String ip, String email) {
super(ip);

this.email = email;
}

public String email() {
return email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tv.codely.ecommerce.login_attempt.domain;

public abstract class LoginAttempt {
private final String ip;

public LoginAttempt(String ip) {
this.ip = ip;
}

public String ip() {
return ip;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tv.codely.ecommerce.login_attempt.domain;

import java.util.HashMap;

public interface LoginAttemptRepository {
HashMap<String, String> save(LoginAttempt loginAttempt) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tv.codely.ecommerce.login_attempt.domain;

public final class TwitterLoginAttempt extends LoginAttempt {
private final String username;

public TwitterLoginAttempt(String ip, String username) {
super(ip);

this.username = username;
}

public String username() {
return username;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tv.codely.ecommerce.login_attempt.domain.format;

import tv.codely.ecommerce.login_attempt.domain.EmailLoginAttempt;

import java.util.HashMap;

public final class EmailLoginAttemptFormatter extends LoginAttemptFormatter<EmailLoginAttempt> {
@Override
public HashMap<String, String> format(EmailLoginAttempt loginAttempt) {
return new HashMap<>() {{
put("ip", loginAttempt.ip());
put("email", loginAttempt.email());
}};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tv.codely.ecommerce.login_attempt.domain.format;

import tv.codely.ecommerce.login_attempt.domain.LoginAttempt;

import java.util.HashMap;

public abstract class LoginAttemptFormatter<T extends LoginAttempt> {
public abstract HashMap<String, String> format(T loginAttempt);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tv.codely.ecommerce.login_attempt.domain.format;

import tv.codely.ecommerce.login_attempt.domain.TwitterLoginAttempt;

import java.util.HashMap;

public final class TwitterLoginAttemptFormatter extends LoginAttemptFormatter<TwitterLoginAttempt> {
@Override
public HashMap<String, String> format(TwitterLoginAttempt loginAttempt) {
return new HashMap<>() {{
put("ip", loginAttempt.ip());
put("username", loginAttempt.username());
}};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tv.codely.ecommerce.login_attempt.infrastructure;

import tv.codely.ecommerce.login_attempt.domain.EmailLoginAttempt;
import tv.codely.ecommerce.login_attempt.domain.LoginAttempt;
import tv.codely.ecommerce.login_attempt.domain.LoginAttemptRepository;
import tv.codely.ecommerce.login_attempt.domain.TwitterLoginAttempt;
import tv.codely.ecommerce.login_attempt.domain.format.EmailLoginAttemptFormatter;
import tv.codely.ecommerce.login_attempt.domain.format.LoginAttemptFormatter;
import tv.codely.ecommerce.login_attempt.domain.format.TwitterLoginAttemptFormatter;

import java.util.HashMap;

public final class SdkAuth0LoginAttemptRepository implements LoginAttemptRepository {
@Override
public HashMap<String, String> save(LoginAttempt loginAttempt) throws Exception {
LoginAttemptFormatter formatter;

if (loginAttempt instanceof TwitterLoginAttempt) {
formatter = new TwitterLoginAttemptFormatter();
} else if (loginAttempt instanceof EmailLoginAttempt) {
formatter = new EmailLoginAttemptFormatter();
} else {
throw new Exception("There are no formatters for that attempt");
}

return formatter.format(loginAttempt);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tv.codely.ecommerce.sign_up;
package tv.codely.ecommerce.sign_up.domain;

public final class EmailSignUpProvider extends SignUpProvider {
private final String email;
private final String password;

public EmailSignUpProvider(String email, String password) {
super("email");

this.email = email;
this.password = password;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tv.codely.ecommerce.sign_up;
package tv.codely.ecommerce.sign_up.domain;

public final class FacebookSignUpProvider extends SignUpProvider {
private final String email;
private final String hash;

public FacebookSignUpProvider(String email, String hash) {
super("facebook");

this.email = email;
this.hash = hash;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tv.codely.ecommerce.sign_up;
package tv.codely.ecommerce.sign_up.domain;

public final class GitHubSignUpProvider extends SignUpProvider {
private final String username;
private final String secretKey;

public GitHubSignUpProvider(String username, String secretKey) {
super("github");

this.username = username;
this.secretKey = secretKey;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tv.codely.ecommerce.sign_up;
package tv.codely.ecommerce.sign_up.domain;

public abstract class SignUpProvider {
private String provider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tv.codely.ecommerce.sign_up;
package tv.codely.ecommerce.sign_up.domain;

public final class TwitterSignUpProvider extends SignUpProvider {
private String username;
private String token;

public TwitterSignUpProvider(String username, String token) {
super("twitter");

this.username = username;
this.token = token;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tv.codely.ecommerce.login_attempt.infrastructure;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import tv.codely.ecommerce.login_attempt.domain.EmailLoginAttempt;
import tv.codely.ecommerce.login_attempt.domain.LoginAttempt;
import tv.codely.ecommerce.login_attempt.domain.TwitterLoginAttempt;

import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

final class SdkAuth0LoginAttemptRepositoryShould {
private SdkAuth0LoginAttemptRepository repository;

@BeforeEach
protected void setUp() {
repository = new SdkAuth0LoginAttemptRepository();
}

@Test
void add_an_email_login_attempt() throws Exception {
EmailLoginAttempt loginAttempt = new EmailLoginAttempt("192.168.1.1", "[email protected]");
HashMap<String, String> expected = new HashMap<>() {{
put("ip", "192.168.1.1");
put("email", "[email protected]");
}};

assertEquals(expected, repository.save(loginAttempt));
}

@Test
void add_a_twitter_login_attempt() throws Exception {
TwitterLoginAttempt loginAttempt = new TwitterLoginAttempt("192.168.1.2", "codelytv");
HashMap<String, String> expected = new HashMap<>() {{
put("ip", "192.168.1.2");
put("username", "codelytv");
}};

assertEquals(expected, repository.save(loginAttempt));
}

@Test
void throw_an_exception_with_an_unknown_login_attempt() {
LoginAttempt loginAttempt = new LoginAttempt("192.168.1.3") {
public String name() {
return "unknown";
}
};

assertThrows(Exception.class, () -> repository.save(loginAttempt));
}
}

0 comments on commit cb77b9f

Please sign in to comment.