Skip to content

Commit

Permalink
Merge pull request Baeldung#231 from psevestre/BAEL-4757
Browse files Browse the repository at this point in the history
Bael 4757
  • Loading branch information
maibin authored Jan 22, 2021
2 parents 0ce81d0 + 2039f69 commit df0e153
Show file tree
Hide file tree
Showing 13 changed files with 2,143 additions and 1 deletion.
95 changes: 95 additions & 0 deletions oauth-rest/keycloack-custom-providers/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.baeldung</groupId>
<artifactId>keycloak-custom-providers</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath />
</parent>

<dependencies>

<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>${keycloak.version}</version>
</dependency>

<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-server-spi</artifactId>
<version>${keycloak.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<!-- Embedded Keycloak sample -->
<dependency>
<groupId>com.baeldung</groupId>
<artifactId>oauth-authorization-server</artifactId>
<version>0.1.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>

<!-- test -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<!-- non-dependencies -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>

<keycloak.version>11.0.2</keycloak.version>

<!-- these should be updated together with Keycloak -->
<!-- check keycloak-dependencies-server-all effective pom -->
<infinispan.version>10.1.8.Final</infinispan.version>
<resteasy.version>3.12.1.Final</resteasy.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.baeldung.auth.provider.user;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.component.ComponentModel;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.storage.adapter.AbstractUserAdapter;


class CustomUser extends AbstractUserAdapter {

private final String username;
private final String email;
private final String firstName;
private final String lastName;
private final Date birthDate;

private CustomUser(KeycloakSession session, RealmModel realm,
ComponentModel storageProviderModel,
String username,
String email,
String firstName,
String lastName,
Date birthDate ) {
super(session, realm, storageProviderModel);
this.username = username;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;

}

@Override
public String getUsername() {
return username;
}

@Override
public String getFirstName() {
return firstName;
}

@Override
public String getLastName() {
return lastName;
}

@Override
public String getEmail() {
return email;
}

public Date getBirthDate() {
return birthDate;
}

@Override
public Map<String, List<String>> getAttributes() {
MultivaluedHashMap<String, String> attributes = new MultivaluedHashMap<>();
attributes.add(UserModel.USERNAME, getUsername());
attributes.add(UserModel.EMAIL,getEmail());
attributes.add(UserModel.FIRST_NAME,getFirstName());
attributes.add(UserModel.LAST_NAME,getLastName());
attributes.add("birthDate",getBirthDate().toString());
return attributes;
}

static class Builder {
private final KeycloakSession session;
private final RealmModel realm;
private final ComponentModel storageProviderModel;
private String username;
private String email;
private String firstName;
private String lastName;
private Date birthDate;

Builder(KeycloakSession session, RealmModel realm, ComponentModel storageProviderModel,String username) {
this.session = session;
this.realm = realm;
this.storageProviderModel = storageProviderModel;
this.username = username;
}

CustomUser.Builder email(String email) {
this.email = email;
return this;
}

CustomUser.Builder firstName(String firstName) {
this.firstName = firstName;
return this;
}

CustomUser.Builder lastName(String lastName) {
this.lastName = lastName;
return this;
}

CustomUser.Builder birthDate(Date birthDate) {
this.birthDate = birthDate;
return this;
}

CustomUser build() {
return new CustomUser(
session,
realm,
storageProviderModel,
username,
email,
firstName,
lastName,
birthDate);

}
}
}
Loading

0 comments on commit df0e153

Please sign in to comment.