KEYCLOAK-359 Add user friendly username for social links
This commit is contained in:
parent
53b596d435
commit
6dc156712e
31 changed files with 226 additions and 83 deletions
|
@ -6,6 +6,7 @@ package org.keycloak.representations.idm;
|
||||||
public class SocialLinkRepresentation {
|
public class SocialLinkRepresentation {
|
||||||
|
|
||||||
protected String socialProvider;
|
protected String socialProvider;
|
||||||
|
protected String socialUserId;
|
||||||
protected String socialUsername;
|
protected String socialUsername;
|
||||||
|
|
||||||
public String getSocialProvider() {
|
public String getSocialProvider() {
|
||||||
|
@ -16,6 +17,14 @@ public class SocialLinkRepresentation {
|
||||||
this.socialProvider = socialProvider;
|
this.socialProvider = socialProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSocialUserId() {
|
||||||
|
return socialUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSocialUserId(String socialUserId) {
|
||||||
|
this.socialUserId = socialUserId;
|
||||||
|
}
|
||||||
|
|
||||||
public String getSocialUsername() {
|
public String getSocialUsername() {
|
||||||
return socialUsername;
|
return socialUsername;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,9 +32,10 @@ public class SocialMappingRepresentation {
|
||||||
return socialLinks;
|
return socialLinks;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SocialLinkRepresentation socialLink(String socialProvider, String socialUsername) {
|
public SocialLinkRepresentation socialLink(String socialProvider, String socialUserId, String socialUsername) {
|
||||||
SocialLinkRepresentation link = new SocialLinkRepresentation();
|
SocialLinkRepresentation link = new SocialLinkRepresentation();
|
||||||
link.setSocialProvider(socialProvider);
|
link.setSocialProvider(socialProvider);
|
||||||
|
link.setSocialUserId(socialUserId);
|
||||||
link.setSocialUsername(socialUsername);
|
link.setSocialUsername(socialUsername);
|
||||||
if (socialLinks == null) socialLinks = new ArrayList<SocialLinkRepresentation>();
|
if (socialLinks == null) socialLinks = new ArrayList<SocialLinkRepresentation>();
|
||||||
socialLinks.add(link);
|
socialLinks.add(link);
|
||||||
|
|
|
@ -40,10 +40,4 @@
|
||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</orderedlist>
|
</orderedlist>
|
||||||
<tip>
|
|
||||||
<para>
|
|
||||||
Facebook doesn't allow <literal>localhost</literal> in the redirect URI. To test on a local server
|
|
||||||
replace <literal>localhost</literal> with <literal>127.0.0.1</literal>.
|
|
||||||
</para>
|
|
||||||
</tip>
|
|
||||||
</section>
|
</section>
|
27
examples/test-cordova.json
Normal file
27
examples/test-cordova.json
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
"id": "test",
|
||||||
|
"realm": "test",
|
||||||
|
"enabled": true,
|
||||||
|
"sslNotRequired": true,
|
||||||
|
"privateKey": "MIICXAIBAAKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQABAoGAfmO8gVhyBxdqlxmIuglbz8bcjQbhXJLR2EoS8ngTXmN1bo2L90M0mUKSdc7qF10LgETBzqL8jYlQIbt+e6TH8fcEpKCjUlyq0Mf/vVbfZSNaVycY13nTzo27iPyWQHK5NLuJzn1xvxxrUeXI6A2WFpGEBLbHjwpx5WQG9A+2scECQQDvdn9NE75HPTVPxBqsEd2z10TKkl9CZxu10Qby3iQQmWLEJ9LNmy3acvKrE3gMiYNWb6xHPKiIqOR1as7L24aTAkEAtyvQOlCvr5kAjVqrEKXalj0Tzewjweuxc0pskvArTI2Oo070h65GpoIKLc9jf+UA69cRtquwP93aZKtW06U8dQJAF2Y44ks/mK5+eyDqik3koCI08qaC8HYq2wVl7G2QkJ6sbAaILtcvD92ToOvyGyeE0flvmDZxMYlvaZnaQ0lcSQJBAKZU6umJi3/xeEbkJqMfeLclD27XGEFoPeNrmdx0q10Azp4NfJAY+Z8KRyQCR2BEG+oNitBOZ+YXF9KCpH3cdmECQHEigJhYg+ykOvr1aiZUMFT72HU0jnmQe2FVekuG+LJUt2Tm7GtMjTFoGpf0JwrVuZN39fOYAlo+nTixgeW7X8Y=",
|
||||||
|
"publicKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQAB",
|
||||||
|
"requiredCredentials": [ "password" ],
|
||||||
|
"users" : [
|
||||||
|
{
|
||||||
|
"username" : "test",
|
||||||
|
"enabled": true,
|
||||||
|
"email" : "test-user@localhost",
|
||||||
|
"credentials" : [
|
||||||
|
{ "type" : "password",
|
||||||
|
"value" : "test" }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"applications": [
|
||||||
|
{
|
||||||
|
"name": "test",
|
||||||
|
"enabled": true,
|
||||||
|
"secret": "password"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -33,22 +33,22 @@ public class AccountSocialBean {
|
||||||
for (SocialProvider provider : SocialLoader.load()) {
|
for (SocialProvider provider : SocialLoader.load()) {
|
||||||
String socialProviderId = provider.getId();
|
String socialProviderId = provider.getId();
|
||||||
if (socialConfig.containsKey(socialProviderId + ".key")) {
|
if (socialConfig.containsKey(socialProviderId + ".key")) {
|
||||||
String socialUsername = getSocialUsername(userSocialLinks, socialProviderId);
|
SocialLinkModel socialLink = getSocialLink(userSocialLinks, socialProviderId);
|
||||||
|
|
||||||
String action = socialUsername!=null ? "remove" : "add";
|
String action = socialLink != null ? "remove" : "add";
|
||||||
String actionUrl = UriBuilder.fromUri(accountSocialUpdateUri).queryParam("action", action).queryParam("provider_id", socialProviderId).build().toString();
|
String actionUrl = UriBuilder.fromUri(accountSocialUpdateUri).queryParam("action", action).queryParam("provider_id", socialProviderId).build().toString();
|
||||||
|
|
||||||
SocialLinkEntry entry = new SocialLinkEntry(socialProviderId, provider.getName(), socialUsername, actionUrl);
|
SocialLinkEntry entry = new SocialLinkEntry(socialLink, provider.getName(), actionUrl);
|
||||||
this.socialLinks.add(entry);
|
this.socialLinks.add(entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getSocialUsername(Set<SocialLinkModel> userSocialLinks, String socialProviderId) {
|
private SocialLinkModel getSocialLink(Set<SocialLinkModel> userSocialLinks, String socialProviderId) {
|
||||||
for (SocialLinkModel link : userSocialLinks) {
|
for (SocialLinkModel link : userSocialLinks) {
|
||||||
if (socialProviderId.equals(link.getSocialProvider())) {
|
if (socialProviderId.equals(link.getSocialProvider())) {
|
||||||
return link.getSocialUsername();
|
return link;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -60,32 +60,34 @@ public class AccountSocialBean {
|
||||||
|
|
||||||
public class SocialLinkEntry {
|
public class SocialLinkEntry {
|
||||||
|
|
||||||
private final String providerId;
|
private SocialLinkModel link;
|
||||||
private final String providerName;
|
private final String providerName;
|
||||||
private final String socialUsername;
|
|
||||||
private final String actionUrl;
|
private final String actionUrl;
|
||||||
|
|
||||||
public SocialLinkEntry(String providerId, String providerName, String socialUsername, String actionUrl) {
|
public SocialLinkEntry(SocialLinkModel link, String providerName, String actionUrl) {
|
||||||
this.providerId = providerId;
|
this.link = link;
|
||||||
this.providerName = providerName;
|
this.providerName = providerName;
|
||||||
this.socialUsername = socialUsername!=null ? socialUsername : "";
|
|
||||||
this.actionUrl = actionUrl;
|
this.actionUrl = actionUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProviderId() {
|
public String getProviderId() {
|
||||||
return providerId;
|
return link != null ? link.getSocialProvider() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getProviderName() {
|
public String getProviderName() {
|
||||||
return providerName;
|
return providerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSocialUserId() {
|
||||||
|
return link != null ? link.getSocialUserId() : null;
|
||||||
|
}
|
||||||
|
|
||||||
public String getSocialUsername() {
|
public String getSocialUsername() {
|
||||||
return socialUsername;
|
return link != null ? link.getSocialUsername() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return !socialUsername.isEmpty();
|
return link != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getActionUrl() {
|
public String getActionUrl() {
|
||||||
|
|
|
@ -11,16 +11,16 @@
|
||||||
<#list social.links as socialLink>
|
<#list social.links as socialLink>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="col-sm-2 col-md-2">
|
<div class="col-sm-2 col-md-2">
|
||||||
<label for="${socialLink.providerId}" class="control-label">${socialLink.providerName}</label>
|
<label for="${socialLink.providerId!}" class="control-label">${socialLink.providerName!}</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-5 col-md-5">
|
<div class="col-sm-5 col-md-5">
|
||||||
<input disabled="true" class="form-control" value="${socialLink.socialUsername}">
|
<input disabled="true" class="form-control" value="${socialLink.socialUsername!}">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-5 col-md-5">
|
<div class="col-sm-5 col-md-5">
|
||||||
<#if socialLink.connected>
|
<#if socialLink.connected>
|
||||||
<a href="${socialLink.actionUrl}" type="submit" class="btn btn-primary btn-lg">Remove ${socialLink.providerName}</a>
|
<a href="${socialLink.actionUrl}" type="submit" class="btn btn-primary btn-lg">Remove ${socialLink.providerName!}</a>
|
||||||
<#else>
|
<#else>
|
||||||
<a href="${socialLink.actionUrl}" type="submit" class="btn btn-primary btn-lg">Add ${socialLink.providerName}</a>
|
<a href="${socialLink.actionUrl}" type="submit" class="btn btn-primary btn-lg">Add ${socialLink.providerName!}</a>
|
||||||
</#if>
|
</#if>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<script src="http://192.168.0.16/js/keycloak.js"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<button onclick="keycloak.login()">Login</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var keycloak = Keycloak({ realm: 'test', clientId: 'test', clientSecret: 'password' });
|
||||||
|
|
||||||
|
keycloak.init(function () {
|
||||||
|
console.debug('Token: ' + keycloak.tokenParsed);
|
||||||
|
console.debug('Realm access: ' + keycloak.realmAccess);
|
||||||
|
console.debug('Resource access: ' + keycloak.resourceAccess);
|
||||||
|
|
||||||
|
keycloak.loadUserProfile(function (profile) {
|
||||||
|
console.debug(profile);
|
||||||
|
}, function (error) {
|
||||||
|
console.debug(error);
|
||||||
|
})
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
8
keycloak.json
Normal file
8
keycloak.json
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"realm" : "test",
|
||||||
|
"realm-public-key" : "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDddHi8/MoYNtdafydQ+e4P0qrvClCW0o/x9fAbZ09dET/VBZCU28M54LmgJT7snXfreyYBpaQpHkW52/tyuEcJ5KO28LIcvObDQFDq3Z7esrovHl4NRETJBF9Xqwt+XLTZF6m37fYzaUK6MVJzUHP9qmu90LYyyvQ+hBJD0GSw1QIDAQAB",
|
||||||
|
"auth-server-url" : "http://localhost:8081/auth",
|
||||||
|
"ssl-not-required" : true,
|
||||||
|
"resource" : "test",
|
||||||
|
"public-client" : true
|
||||||
|
}
|
|
@ -5,11 +5,29 @@ package org.keycloak.models;
|
||||||
*/
|
*/
|
||||||
public class SocialLinkModel {
|
public class SocialLinkModel {
|
||||||
|
|
||||||
private String socialUsername;
|
private String socialUserId;
|
||||||
private String socialProvider;
|
private String socialProvider;
|
||||||
|
private String socialUsername;
|
||||||
|
|
||||||
public SocialLinkModel(String socialProvider, String socialUsername) {
|
public SocialLinkModel(String socialProvider, String socialUserId, String socialUsername) {
|
||||||
|
this.socialUserId = socialUserId;
|
||||||
|
this.socialProvider = socialProvider;
|
||||||
this.socialUsername = socialUsername;
|
this.socialUsername = socialUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSocialUserId() {
|
||||||
|
return socialUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSocialUserId(String socialUserId) {
|
||||||
|
this.socialUserId = socialUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSocialProvider() {
|
||||||
|
return socialProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSocialProvider(String socialProvider) {
|
||||||
this.socialProvider = socialProvider;
|
this.socialProvider = socialProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,12 +38,4 @@ public class SocialLinkModel {
|
||||||
public void setSocialUsername(String socialUsername) {
|
public void setSocialUsername(String socialUsername) {
|
||||||
this.socialUsername = socialUsername;
|
this.socialUsername = socialUsername;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSocialProvider() {
|
|
||||||
return socialProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSocialProvider(String socialProvider) {
|
|
||||||
this.socialProvider = socialProvider;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -543,13 +543,13 @@ public class RealmAdapter implements RealmModel {
|
||||||
TypedQuery<UserEntity> query = em.createNamedQuery("findUserByLinkAndRealm", UserEntity.class);
|
TypedQuery<UserEntity> query = em.createNamedQuery("findUserByLinkAndRealm", UserEntity.class);
|
||||||
query.setParameter("realm", realm);
|
query.setParameter("realm", realm);
|
||||||
query.setParameter("socialProvider", socialLink.getSocialProvider());
|
query.setParameter("socialProvider", socialLink.getSocialProvider());
|
||||||
query.setParameter("socialUsername", socialLink.getSocialUsername());
|
query.setParameter("socialUserId", socialLink.getSocialUserId());
|
||||||
List<UserEntity> results = query.getResultList();
|
List<UserEntity> results = query.getResultList();
|
||||||
if (results.isEmpty()) {
|
if (results.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
} else if (results.size() > 1) {
|
} else if (results.size() > 1) {
|
||||||
throw new IllegalStateException("More results found for socialProvider=" + socialLink.getSocialProvider() +
|
throw new IllegalStateException("More results found for socialProvider=" + socialLink.getSocialProvider() +
|
||||||
", socialUsername=" + socialLink.getSocialUsername() + ", results=" + results);
|
", socialUserId=" + socialLink.getSocialUserId() + ", results=" + results);
|
||||||
} else {
|
} else {
|
||||||
UserEntity user = results.get(0);
|
UserEntity user = results.get(0);
|
||||||
return new UserAdapter(user);
|
return new UserAdapter(user);
|
||||||
|
@ -563,7 +563,7 @@ public class RealmAdapter implements RealmModel {
|
||||||
List<SocialLinkEntity> results = query.getResultList();
|
List<SocialLinkEntity> results = query.getResultList();
|
||||||
Set<SocialLinkModel> set = new HashSet<SocialLinkModel>();
|
Set<SocialLinkModel> set = new HashSet<SocialLinkModel>();
|
||||||
for (SocialLinkEntity entity : results) {
|
for (SocialLinkEntity entity : results) {
|
||||||
set.add(new SocialLinkModel(entity.getSocialProvider(), entity.getSocialUsername()));
|
set.add(new SocialLinkModel(entity.getSocialProvider(), entity.getSocialUserId(), entity.getSocialUsername()));
|
||||||
}
|
}
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
|
@ -571,7 +571,7 @@ public class RealmAdapter implements RealmModel {
|
||||||
@Override
|
@Override
|
||||||
public SocialLinkModel getSocialLink(UserModel user, String socialProvider) {
|
public SocialLinkModel getSocialLink(UserModel user, String socialProvider) {
|
||||||
SocialLinkEntity entity = findSocialLink(user, socialProvider);
|
SocialLinkEntity entity = findSocialLink(user, socialProvider);
|
||||||
return (entity != null) ? new SocialLinkModel(entity.getSocialProvider(), entity.getSocialUsername()) : null;
|
return (entity != null) ? new SocialLinkModel(entity.getSocialProvider(), entity.getSocialUserId(), entity.getSocialUsername()) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -579,6 +579,7 @@ public class RealmAdapter implements RealmModel {
|
||||||
SocialLinkEntity entity = new SocialLinkEntity();
|
SocialLinkEntity entity = new SocialLinkEntity();
|
||||||
entity.setRealm(realm);
|
entity.setRealm(realm);
|
||||||
entity.setSocialProvider(socialLink.getSocialProvider());
|
entity.setSocialProvider(socialLink.getSocialProvider());
|
||||||
|
entity.setSocialUserId(socialLink.getSocialUserId());
|
||||||
entity.setSocialUsername(socialLink.getSocialUsername());
|
entity.setSocialUsername(socialLink.getSocialUsername());
|
||||||
entity.setUser(((UserAdapter) user).getUser());
|
entity.setUser(((UserAdapter) user).getUser());
|
||||||
em.persist(entity);
|
em.persist(entity);
|
||||||
|
|
|
@ -16,7 +16,7 @@ import org.hibernate.annotations.GenericGenerator;
|
||||||
@NamedQueries({
|
@NamedQueries({
|
||||||
@NamedQuery(name="findSocialLinkByUser", query="select link from SocialLinkEntity link where link.user = :user"),
|
@NamedQuery(name="findSocialLinkByUser", query="select link from SocialLinkEntity link where link.user = :user"),
|
||||||
@NamedQuery(name="findSocialLinkByUserAndProvider", query="select link from SocialLinkEntity link where link.user = :user and link.socialProvider = :socialProvider"),
|
@NamedQuery(name="findSocialLinkByUserAndProvider", query="select link from SocialLinkEntity link where link.user = :user and link.socialProvider = :socialProvider"),
|
||||||
@NamedQuery(name="findUserByLinkAndRealm", query="select link.user from SocialLinkEntity link where link.realm = :realm and link.socialProvider = :socialProvider and link.socialUsername = :socialUsername")
|
@NamedQuery(name="findUserByLinkAndRealm", query="select link.user from SocialLinkEntity link where link.realm = :realm and link.socialProvider = :socialProvider and link.socialUserId = :socialUserId")
|
||||||
})
|
})
|
||||||
@Entity
|
@Entity
|
||||||
public class SocialLinkEntity {
|
public class SocialLinkEntity {
|
||||||
|
@ -32,6 +32,7 @@ public class SocialLinkEntity {
|
||||||
protected RealmEntity realm;
|
protected RealmEntity realm;
|
||||||
|
|
||||||
protected String socialProvider;
|
protected String socialProvider;
|
||||||
|
protected String socialUserId;
|
||||||
protected String socialUsername;
|
protected String socialUsername;
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
|
@ -58,6 +59,14 @@ public class SocialLinkEntity {
|
||||||
this.socialProvider = socialProvider;
|
this.socialProvider = socialProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSocialUserId() {
|
||||||
|
return socialUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSocialUserId(String socialUserId) {
|
||||||
|
this.socialUserId = socialUserId;
|
||||||
|
}
|
||||||
|
|
||||||
public String getSocialUsername() {
|
public String getSocialUsername() {
|
||||||
return socialUsername;
|
return socialUsername;
|
||||||
}
|
}
|
||||||
|
|
|
@ -853,7 +853,7 @@ public class RealmAdapter extends AbstractMongoAdapter<RealmEntity> implements R
|
||||||
public UserModel getUserBySocialLink(SocialLinkModel socialLink) {
|
public UserModel getUserBySocialLink(SocialLinkModel socialLink) {
|
||||||
DBObject query = new QueryBuilder()
|
DBObject query = new QueryBuilder()
|
||||||
.and("socialLinks.socialProvider").is(socialLink.getSocialProvider())
|
.and("socialLinks.socialProvider").is(socialLink.getSocialProvider())
|
||||||
.and("socialLinks.socialUsername").is(socialLink.getSocialUsername())
|
.and("socialLinks.socialUserId").is(socialLink.getSocialUserId())
|
||||||
.and("realmId").is(getId())
|
.and("realmId").is(getId())
|
||||||
.get();
|
.get();
|
||||||
UserEntity userEntity = getMongoStore().loadSingleEntity(UserEntity.class, query, invocationContext);
|
UserEntity userEntity = getMongoStore().loadSingleEntity(UserEntity.class, query, invocationContext);
|
||||||
|
@ -871,7 +871,7 @@ public class RealmAdapter extends AbstractMongoAdapter<RealmEntity> implements R
|
||||||
|
|
||||||
Set<SocialLinkModel> result = new HashSet<SocialLinkModel>();
|
Set<SocialLinkModel> result = new HashSet<SocialLinkModel>();
|
||||||
for (SocialLinkEntity socialLinkEntity : linkEntities) {
|
for (SocialLinkEntity socialLinkEntity : linkEntities) {
|
||||||
SocialLinkModel model = new SocialLinkModel(socialLinkEntity.getSocialProvider(), socialLinkEntity.getSocialUsername());
|
SocialLinkModel model = new SocialLinkModel(socialLinkEntity.getSocialProvider(), socialLinkEntity.getSocialUserId(), socialLinkEntity.getSocialUsername());
|
||||||
result.add(model);
|
result.add(model);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -880,7 +880,7 @@ public class RealmAdapter extends AbstractMongoAdapter<RealmEntity> implements R
|
||||||
@Override
|
@Override
|
||||||
public SocialLinkModel getSocialLink(UserModel user, String socialProvider) {
|
public SocialLinkModel getSocialLink(UserModel user, String socialProvider) {
|
||||||
SocialLinkEntity socialLinkEntity = findSocialLink(user, socialProvider);
|
SocialLinkEntity socialLinkEntity = findSocialLink(user, socialProvider);
|
||||||
return socialLinkEntity!=null ? new SocialLinkModel(socialLinkEntity.getSocialProvider(), socialLinkEntity.getSocialUsername()) : null;
|
return socialLinkEntity!=null ? new SocialLinkModel(socialLinkEntity.getSocialProvider(), socialLinkEntity.getSocialUserId(), socialLinkEntity.getSocialUsername()) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -888,6 +888,7 @@ public class RealmAdapter extends AbstractMongoAdapter<RealmEntity> implements R
|
||||||
UserEntity userEntity = ((UserAdapter)user).getUser();
|
UserEntity userEntity = ((UserAdapter)user).getUser();
|
||||||
SocialLinkEntity socialLinkEntity = new SocialLinkEntity();
|
SocialLinkEntity socialLinkEntity = new SocialLinkEntity();
|
||||||
socialLinkEntity.setSocialProvider(socialLink.getSocialProvider());
|
socialLinkEntity.setSocialProvider(socialLink.getSocialProvider());
|
||||||
|
socialLinkEntity.setSocialUserId(socialLink.getSocialUserId());
|
||||||
socialLinkEntity.setSocialUsername(socialLink.getSocialUsername());
|
socialLinkEntity.setSocialUsername(socialLink.getSocialUsername());
|
||||||
|
|
||||||
getMongoStore().pushItemToList(userEntity, "socialLinks", socialLinkEntity, true, invocationContext);
|
getMongoStore().pushItemToList(userEntity, "socialLinks", socialLinkEntity, true, invocationContext);
|
||||||
|
|
|
@ -8,9 +8,19 @@ import org.keycloak.models.mongo.api.MongoField;
|
||||||
*/
|
*/
|
||||||
public class SocialLinkEntity implements MongoEntity {
|
public class SocialLinkEntity implements MongoEntity {
|
||||||
|
|
||||||
|
private String socialUserId;
|
||||||
private String socialUsername;
|
private String socialUsername;
|
||||||
private String socialProvider;
|
private String socialProvider;
|
||||||
|
|
||||||
|
@MongoField
|
||||||
|
public String getSocialUserId() {
|
||||||
|
return socialUserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSocialUserId(String socialUserId) {
|
||||||
|
this.socialUserId = socialUserId;
|
||||||
|
}
|
||||||
|
|
||||||
@MongoField
|
@MongoField
|
||||||
public String getSocialUsername() {
|
public String getSocialUsername() {
|
||||||
return socialUsername;
|
return socialUsername;
|
||||||
|
@ -37,9 +47,9 @@ public class SocialLinkEntity implements MongoEntity {
|
||||||
SocialLinkEntity that = (SocialLinkEntity) o;
|
SocialLinkEntity that = (SocialLinkEntity) o;
|
||||||
|
|
||||||
if (socialProvider != null && (that.socialProvider == null || !socialProvider.equals(that.socialProvider))) return false;
|
if (socialProvider != null && (that.socialProvider == null || !socialProvider.equals(that.socialProvider))) return false;
|
||||||
if (socialUsername != null && (that.socialUsername == null || !socialUsername.equals(that.socialUsername))) return false;
|
if (socialUserId != null && (that.socialUserId == null || !socialUserId.equals(that.socialUserId))) return false;
|
||||||
if (socialProvider == null && that.socialProvider != null)return false;
|
if (socialProvider == null && that.socialProvider != null)return false;
|
||||||
if (socialUsername == null && that.socialUsername != null) return false;
|
if (socialUserId == null && that.socialUserId != null) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -47,7 +57,7 @@ public class SocialLinkEntity implements MongoEntity {
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int code = 1;
|
int code = 1;
|
||||||
if (socialUsername != null) {
|
if (socialUserId != null) {
|
||||||
code = code * 13;
|
code = code * 13;
|
||||||
}
|
}
|
||||||
if (socialProvider != null) {
|
if (socialProvider != null) {
|
||||||
|
|
|
@ -880,14 +880,14 @@ public class RealmAdapter implements RealmModel {
|
||||||
public UserModel getUserBySocialLink(SocialLinkModel socialLink) {
|
public UserModel getUserBySocialLink(SocialLinkModel socialLink) {
|
||||||
RelationshipQuery<SocialLinkRelationship> query = getRelationshipManager().createRelationshipQuery(SocialLinkRelationship.class);
|
RelationshipQuery<SocialLinkRelationship> query = getRelationshipManager().createRelationshipQuery(SocialLinkRelationship.class);
|
||||||
query.setParameter(SocialLinkRelationship.SOCIAL_PROVIDER, socialLink.getSocialProvider());
|
query.setParameter(SocialLinkRelationship.SOCIAL_PROVIDER, socialLink.getSocialProvider());
|
||||||
query.setParameter(SocialLinkRelationship.SOCIAL_USERNAME, socialLink.getSocialUsername());
|
query.setParameter(SocialLinkRelationship.SOCIAL_USERNAME, socialLink.getSocialUserId());
|
||||||
query.setParameter(SocialLinkRelationship.REALM, realm.getName());
|
query.setParameter(SocialLinkRelationship.REALM, realm.getName());
|
||||||
List<SocialLinkRelationship> results = query.getResultList();
|
List<SocialLinkRelationship> results = query.getResultList();
|
||||||
if (results.isEmpty()) {
|
if (results.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
} else if (results.size() > 1) {
|
} else if (results.size() > 1) {
|
||||||
throw new IllegalStateException("More results found for socialProvider=" + socialLink.getSocialProvider() +
|
throw new IllegalStateException("More results found for socialProvider=" + socialLink.getSocialProvider() +
|
||||||
", socialUsername=" + socialLink.getSocialUsername() + ", results=" + results);
|
", socialUserId=" + socialLink.getSocialUserId() + ", results=" + results);
|
||||||
} else {
|
} else {
|
||||||
User user = results.get(0).getUser();
|
User user = results.get(0).getUser();
|
||||||
return new UserAdapter(user, getIdm());
|
return new UserAdapter(user, getIdm());
|
||||||
|
@ -902,7 +902,7 @@ public class RealmAdapter implements RealmModel {
|
||||||
|
|
||||||
Set<SocialLinkModel> results = new HashSet<SocialLinkModel>();
|
Set<SocialLinkModel> results = new HashSet<SocialLinkModel>();
|
||||||
for (SocialLinkRelationship relationship : plSocialLinks) {
|
for (SocialLinkRelationship relationship : plSocialLinks) {
|
||||||
results.add(new SocialLinkModel(relationship.getSocialProvider(), relationship.getSocialUsername()));
|
results.add(new SocialLinkModel(relationship.getSocialProvider(), relationship.getSocialUserId()));
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
@ -912,7 +912,7 @@ public class RealmAdapter implements RealmModel {
|
||||||
SocialLinkRelationship relationship = new SocialLinkRelationship();
|
SocialLinkRelationship relationship = new SocialLinkRelationship();
|
||||||
relationship.setUser(((UserAdapter)user).getUser());
|
relationship.setUser(((UserAdapter)user).getUser());
|
||||||
relationship.setSocialProvider(socialLink.getSocialProvider());
|
relationship.setSocialProvider(socialLink.getSocialProvider());
|
||||||
relationship.setSocialUsername(socialLink.getSocialUsername());
|
relationship.setSocialUserId(socialLink.getSocialUserId());
|
||||||
relationship.setRealm(realm.getName());
|
relationship.setRealm(realm.getName());
|
||||||
|
|
||||||
getRelationshipManager().add(relationship);
|
getRelationshipManager().add(relationship);
|
||||||
|
@ -923,7 +923,7 @@ public class RealmAdapter implements RealmModel {
|
||||||
SocialLinkRelationship relationship = new SocialLinkRelationship();
|
SocialLinkRelationship relationship = new SocialLinkRelationship();
|
||||||
relationship.setUser(((UserAdapter)user).getUser());
|
relationship.setUser(((UserAdapter)user).getUser());
|
||||||
relationship.setSocialProvider(socialLink.getSocialProvider());
|
relationship.setSocialProvider(socialLink.getSocialProvider());
|
||||||
relationship.setSocialUsername(socialLink.getSocialUsername());
|
relationship.setSocialUserId(socialLink.getSocialUserId());
|
||||||
relationship.setRealm(realm.getName());
|
relationship.setRealm(realm.getName());
|
||||||
|
|
||||||
getRelationshipManager().remove(relationship);
|
getRelationshipManager().remove(relationship);
|
||||||
|
|
|
@ -20,9 +20,9 @@ public class SocialLinkRelationship extends AbstractAttributedType implements Re
|
||||||
private static final long serialVersionUID = 154879L;
|
private static final long serialVersionUID = 154879L;
|
||||||
|
|
||||||
public static final AttributeParameter SOCIAL_PROVIDER = new AttributeParameter("socialProvider");
|
public static final AttributeParameter SOCIAL_PROVIDER = new AttributeParameter("socialProvider");
|
||||||
public static final AttributeParameter SOCIAL_USERNAME = new AttributeParameter("socialUsername");
|
public static final AttributeParameter SOCIAL_USERID = new AttributeParameter("socialUserId");
|
||||||
|
|
||||||
// realm is needed to allow searching as combination socialUsername+socialProvider may not be unique
|
// realm is needed to allow searching as combination socialUserId+socialProvider may not be unique
|
||||||
// (Same user could have mapped same facebook account to username "foo" in "realm1" and to username "bar" in "realm2")
|
// (Same user could have mapped same facebook account to username "foo" in "realm1" and to username "bar" in "realm2")
|
||||||
public static final AttributeParameter REALM = new AttributeParameter("realm");
|
public static final AttributeParameter REALM = new AttributeParameter("realm");
|
||||||
|
|
||||||
|
@ -54,12 +54,12 @@ public class SocialLinkRelationship extends AbstractAttributedType implements Re
|
||||||
}
|
}
|
||||||
|
|
||||||
@AttributeProperty
|
@AttributeProperty
|
||||||
public String getSocialUsername() {
|
public String getSocialUserId() {
|
||||||
return (String)getAttribute("socialUsername").getValue();
|
return (String)getAttribute("socialUserId").getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSocialUsername(String socialProviderUserId) {
|
public void setSocialUserId(String socialUserId) {
|
||||||
setAttribute(new Attribute<String>("socialUsername", socialProviderUserId));
|
setAttribute(new Attribute<String>("socialUserId", socialUserId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@AttributeProperty
|
@AttributeProperty
|
||||||
|
|
|
@ -156,7 +156,7 @@ public class AdapterTest extends AbstractModelTest {
|
||||||
RoleModel appRole = app.addRole("test");
|
RoleModel appRole = app.addRole("test");
|
||||||
realmModel.grantRole(user, appRole);
|
realmModel.grantRole(user, appRole);
|
||||||
|
|
||||||
SocialLinkModel socialLink = new SocialLinkModel("google", user.getLoginName());
|
SocialLinkModel socialLink = new SocialLinkModel("google", "google1", user.getLoginName());
|
||||||
realmModel.addSocialLink(user, socialLink);
|
realmModel.addSocialLink(user, socialLink);
|
||||||
|
|
||||||
UserCredentialModel cred = new UserCredentialModel();
|
UserCredentialModel cred = new UserCredentialModel();
|
||||||
|
|
|
@ -133,22 +133,26 @@ public class ImportTest extends AbstractModelTest {
|
||||||
for (SocialLinkModel socialLinkModel : socialLinks) {
|
for (SocialLinkModel socialLinkModel : socialLinks) {
|
||||||
if ("facebook".equals(socialLinkModel.getSocialProvider())) {
|
if ("facebook".equals(socialLinkModel.getSocialProvider())) {
|
||||||
facebookFound = true;
|
facebookFound = true;
|
||||||
|
Assert.assertEquals(socialLinkModel.getSocialUserId(), "facebook1");
|
||||||
Assert.assertEquals(socialLinkModel.getSocialUsername(), "fbuser1");
|
Assert.assertEquals(socialLinkModel.getSocialUsername(), "fbuser1");
|
||||||
} else if ("google".equals(socialLinkModel.getSocialProvider())) {
|
} else if ("google".equals(socialLinkModel.getSocialProvider())) {
|
||||||
googleFound = true;
|
googleFound = true;
|
||||||
|
Assert.assertEquals(socialLinkModel.getSocialUserId(), "google1");
|
||||||
Assert.assertEquals(socialLinkModel.getSocialUsername(), "mySocialUser@gmail.com");
|
Assert.assertEquals(socialLinkModel.getSocialUsername(), "mySocialUser@gmail.com");
|
||||||
} else if ("twitter".equals(socialLinkModel.getSocialProvider())) {
|
} else if ("twitter".equals(socialLinkModel.getSocialProvider())) {
|
||||||
twitterFound = true;
|
twitterFound = true;
|
||||||
|
Assert.assertEquals(socialLinkModel.getSocialUserId(), "twitter1");
|
||||||
Assert.assertEquals(socialLinkModel.getSocialUsername(), "twuser1");
|
Assert.assertEquals(socialLinkModel.getSocialUsername(), "twuser1");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Assert.assertTrue(facebookFound && twitterFound && googleFound);
|
Assert.assertTrue(facebookFound && twitterFound && googleFound);
|
||||||
|
|
||||||
UserModel foundSocialUser = realm.getUserBySocialLink(new SocialLinkModel("facebook", "fbuser1"));
|
UserModel foundSocialUser = realm.getUserBySocialLink(new SocialLinkModel("facebook", "facebook1", "fbuser1"));
|
||||||
Assert.assertEquals(foundSocialUser.getLoginName(), socialUser.getLoginName());
|
Assert.assertEquals(foundSocialUser.getLoginName(), socialUser.getLoginName());
|
||||||
Assert.assertNull(realm.getUserBySocialLink(new SocialLinkModel("facebook", "not-existing")));
|
Assert.assertNull(realm.getUserBySocialLink(new SocialLinkModel("facebook", "not-existing", "not-existing")));
|
||||||
|
|
||||||
SocialLinkModel foundSocialLink = realm.getSocialLink(socialUser, "facebook");
|
SocialLinkModel foundSocialLink = realm.getSocialLink(socialUser, "facebook");
|
||||||
|
Assert.assertEquals("facebook1", foundSocialLink.getSocialUserId());
|
||||||
Assert.assertEquals("fbuser1", foundSocialLink.getSocialUsername());
|
Assert.assertEquals("fbuser1", foundSocialLink.getSocialUsername());
|
||||||
Assert.assertEquals("facebook", foundSocialLink.getSocialProvider());
|
Assert.assertEquals("facebook", foundSocialLink.getSocialProvider());
|
||||||
|
|
||||||
|
|
|
@ -52,14 +52,17 @@
|
||||||
"socialLinks": [
|
"socialLinks": [
|
||||||
{
|
{
|
||||||
"socialProvider": "facebook",
|
"socialProvider": "facebook",
|
||||||
|
"socialUserId": "facebook1",
|
||||||
"socialUsername": "fbuser1"
|
"socialUsername": "fbuser1"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"socialProvider": "twitter",
|
"socialProvider": "twitter",
|
||||||
|
"socialUserId": "twitter1",
|
||||||
"socialUsername": "twuser1"
|
"socialUsername": "twuser1"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"socialProvider": "google",
|
"socialProvider": "google",
|
||||||
|
"socialUserId": "google1",
|
||||||
"socialUsername": "mySocialUser@gmail.com"
|
"socialUsername": "mySocialUser@gmail.com"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -366,7 +366,7 @@ public class RealmManager {
|
||||||
for (SocialMappingRepresentation socialMapping : rep.getSocialMappings()) {
|
for (SocialMappingRepresentation socialMapping : rep.getSocialMappings()) {
|
||||||
UserModel user = userMap.get(socialMapping.getUsername());
|
UserModel user = userMap.get(socialMapping.getUsername());
|
||||||
for (SocialLinkRepresentation link : socialMapping.getSocialLinks()) {
|
for (SocialLinkRepresentation link : socialMapping.getSocialLinks()) {
|
||||||
SocialLinkModel mappingModel = new SocialLinkModel(link.getSocialProvider(), link.getSocialUsername());
|
SocialLinkModel mappingModel = new SocialLinkModel(link.getSocialProvider(), link.getSocialUserId(), link.getSocialUsername());
|
||||||
newRealm.addSocialLink(user, mappingModel);
|
newRealm.addSocialLink(user, mappingModel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,7 +146,7 @@ public class SocialResource {
|
||||||
return oauth.forwardToSecurityFailure("Failed to process social callback");
|
return oauth.forwardToSecurityFailure("Failed to process social callback");
|
||||||
}
|
}
|
||||||
|
|
||||||
SocialLinkModel socialLink = new SocialLinkModel(provider.getId(), socialUser.getId());
|
SocialLinkModel socialLink = new SocialLinkModel(provider.getId(), socialUser.getId(), socialUser.getUsername());
|
||||||
UserModel user = realm.getUserBySocialLink(socialLink);
|
UserModel user = realm.getUserBySocialLink(socialLink);
|
||||||
|
|
||||||
// Check if user is already authenticated (this means linking social into existing user account)
|
// Check if user is already authenticated (this means linking social into existing user account)
|
||||||
|
|
|
@ -43,7 +43,7 @@ public class AuthCallback {
|
||||||
|
|
||||||
public String getQueryParam(String name) {
|
public String getQueryParam(String name) {
|
||||||
String[] value = queryParams.get(name);
|
String[] value = queryParams.get(name);
|
||||||
if (value.length > 0) {
|
if (value != null && value.length > 0) {
|
||||||
return value[0];
|
return value[0];
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -3,12 +3,14 @@ package org.keycloak.social;
|
||||||
public class SocialUser {
|
public class SocialUser {
|
||||||
|
|
||||||
private String id;
|
private String id;
|
||||||
|
private String username;
|
||||||
private String firstName;
|
private String firstName;
|
||||||
private String lastName;
|
private String lastName;
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
public SocialUser(String id) {
|
public SocialUser(String id, String username) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getId() {
|
public String getId() {
|
||||||
|
@ -19,6 +21,14 @@ public class SocialUser {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
public String getFirstName() {
|
public String getFirstName() {
|
||||||
return firstName;
|
return firstName;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,7 @@ public class FacebookProvider extends AbstractOAuth2Provider {
|
||||||
try {
|
try {
|
||||||
JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson();
|
JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson();
|
||||||
|
|
||||||
SocialUser user = new SocialUser(profile.getString("id"));
|
SocialUser user = new SocialUser(profile.getString("id"), profile.getString("username"));
|
||||||
user.setName(profile.optString("first_name"), profile.optString("last_name"));
|
user.setName(profile.optString("first_name"), profile.optString("last_name"));
|
||||||
user.setEmail(profile.optString("email"));
|
user.setEmail(profile.optString("email"));
|
||||||
|
|
||||||
|
@ -64,10 +64,6 @@ public class FacebookProvider extends AbstractOAuth2Provider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuthRequest getAuthUrl(SocialProviderConfig config) throws SocialProviderException {
|
public AuthRequest getAuthUrl(SocialProviderConfig config) throws SocialProviderException {
|
||||||
if (config.getCallbackUrl().contains("//localhost")) {
|
|
||||||
String callbackUrl = config.getCallbackUrl().replace("//localhost", "//127.0.0.1");
|
|
||||||
config = new SocialProviderConfig(config.getKey(), config.getSecret(), callbackUrl);
|
|
||||||
}
|
|
||||||
return super.getAuthUrl(config);
|
return super.getAuthUrl(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -52,7 +52,7 @@ public class GitHubProvider extends AbstractOAuth2Provider {
|
||||||
try {
|
try {
|
||||||
JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson();
|
JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson();
|
||||||
|
|
||||||
SocialUser user = new SocialUser(profile.get("id").toString());
|
SocialUser user = new SocialUser(profile.get("id").toString(), profile.getString("login"));
|
||||||
user.setName(profile.optString("name"));
|
user.setName(profile.optString("name"));
|
||||||
user.setEmail(profile.optString("email"));
|
user.setEmail(profile.optString("email"));
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ public class GoogleProvider extends AbstractOAuth2Provider {
|
||||||
try {
|
try {
|
||||||
JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson();
|
JSONObject profile = SimpleHttp.doGet(PROFILE_URL).header("Authorization", "Bearer " + accessToken).asJson();
|
||||||
|
|
||||||
SocialUser user = new SocialUser(profile.getString("sub"));
|
SocialUser user = new SocialUser(profile.getString("sub"), profile.getString("email"));
|
||||||
user.setName(profile.optString("given_name"), profile.optString("family_name"));
|
user.setName(profile.optString("given_name"), profile.optString("family_name"));
|
||||||
user.setEmail(profile.optString("email"));
|
user.setEmail(profile.optString("email"));
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ public class TwitterProvider implements SocialProvider {
|
||||||
twitter.getOAuthAccessToken(requestToken, verifier);
|
twitter.getOAuthAccessToken(requestToken, verifier);
|
||||||
twitter4j.User twitterUser = twitter.verifyCredentials();
|
twitter4j.User twitterUser = twitter.verifyCredentials();
|
||||||
|
|
||||||
SocialUser user = new SocialUser(Long.toString(twitterUser.getId()));
|
SocialUser user = new SocialUser(Long.toString(twitterUser.getId()), twitterUser.getScreenName());
|
||||||
user.setName(twitterUser.getName());
|
user.setName(twitterUser.getName());
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
|
|
|
@ -42,8 +42,9 @@ public class DummySocial implements SocialProvider {
|
||||||
throw new SocialProviderException("Invalid state");
|
throw new SocialProviderException("Invalid state");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String id = callback.getQueryParam("id");
|
||||||
String username = callback.getQueryParam("username");
|
String username = callback.getQueryParam("username");
|
||||||
SocialUser user = new SocialUser(username);
|
SocialUser user = new SocialUser(id, username);
|
||||||
user.setName(callback.getQueryParam("firstname"), callback.getQueryParam("lastname"));
|
user.setName(callback.getQueryParam("firstname"), callback.getQueryParam("lastname"));
|
||||||
user.setEmail(callback.getQueryParam("email"));
|
user.setEmail(callback.getQueryParam("email"));
|
||||||
return user;
|
return user;
|
||||||
|
|
|
@ -22,11 +22,13 @@ public class DummySocialServlet extends HttpServlet {
|
||||||
pw.print("<html>");
|
pw.print("<html>");
|
||||||
pw.print("<body>");
|
pw.print("<body>");
|
||||||
pw.print("<form method=\"post\">");
|
pw.print("<form method=\"post\">");
|
||||||
|
pw.print("<label for=\"id\">ID</label><input type=\"text\" id=\"id\" name=\"id\" />");
|
||||||
pw.print("<label for=\"username\">Username</label><input type=\"text\" id=\"username\" name=\"username\" />");
|
pw.print("<label for=\"username\">Username</label><input type=\"text\" id=\"username\" name=\"username\" />");
|
||||||
pw.print("<label for=\"firstname\">First Name</label><input type=\"text\" id=\"firstname\" name=\"firstname\" />");
|
pw.print("<label for=\"firstname\">First Name</label><input type=\"text\" id=\"firstname\" name=\"firstname\" />");
|
||||||
pw.print("<label for=\"lastname\">Last Name</label><input type=\"text\" id=\"lastname\" name=\"lastname\" />");
|
pw.print("<label for=\"lastname\">Last Name</label><input type=\"text\" id=\"lastname\" name=\"lastname\" />");
|
||||||
pw.print("<label for=\"email\">Email</label><input type=\"text\" id=\"email\" name=\"email\" />");
|
pw.print("<label for=\"email\">Email</label><input type=\"text\" id=\"email\" name=\"email\" />");
|
||||||
pw.print("<input type=\"submit\" id=\"submit\" value=\"login\" />");
|
pw.print("<input type=\"submit\" id=\"submit\" value=\"login\" />");
|
||||||
|
pw.print("<input type=\"submit\" id=\"cancel\" value=\"cancel\" />");
|
||||||
pw.print("</form>");
|
pw.print("</form>");
|
||||||
pw.print("</body>");
|
pw.print("</body>");
|
||||||
pw.print("</html>");
|
pw.print("</html>");
|
||||||
|
@ -53,7 +55,9 @@ public class DummySocialServlet extends HttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String redirect = redirectUri + "?username=" + req.getParameter("username") + "&state=" + state + "&code=" + UUID.randomUUID().toString();
|
String redirect;
|
||||||
|
if (req.getParameter("login") != null) {
|
||||||
|
redirect = redirectUri + "?id=" + req.getParameter("id") + "&username=" + req.getParameter("username") + "&state=" + state + "&code=" + UUID.randomUUID().toString();
|
||||||
if (req.getParameter("firstname") != null) {
|
if (req.getParameter("firstname") != null) {
|
||||||
redirect += "&firstname=" + req.getParameter("firstname");
|
redirect += "&firstname=" + req.getParameter("firstname");
|
||||||
}
|
}
|
||||||
|
@ -63,6 +67,9 @@ public class DummySocialServlet extends HttpServlet {
|
||||||
if (req.getParameter("email") != null) {
|
if (req.getParameter("email") != null) {
|
||||||
redirect += "&email=" + req.getParameter("email");
|
redirect += "&email=" + req.getParameter("email");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
redirect = redirectUri + "?error=access_denied&state=" + state;
|
||||||
|
}
|
||||||
|
|
||||||
resp.sendRedirect(redirect);
|
resp.sendRedirect(redirect);
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,6 +97,7 @@ public class SocialLoginTest {
|
||||||
|
|
||||||
loginPage.clickSocial("dummy");
|
loginPage.clickSocial("dummy");
|
||||||
|
|
||||||
|
driver.findElement(By.id("id")).sendKeys("1");
|
||||||
driver.findElement(By.id("username")).sendKeys("dummy-user1");
|
driver.findElement(By.id("username")).sendKeys("dummy-user1");
|
||||||
driver.findElement(By.id("firstname")).sendKeys("Bob");
|
driver.findElement(By.id("firstname")).sendKeys("Bob");
|
||||||
driver.findElement(By.id("lastname")).sendKeys("Builder");
|
driver.findElement(By.id("lastname")).sendKeys("Builder");
|
||||||
|
@ -118,6 +119,30 @@ public class SocialLoginTest {
|
||||||
Assert.assertEquals("bob@builder.com", profile.getEmail());
|
Assert.assertEquals("bob@builder.com", profile.getEmail());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loginCancelled() throws Exception {
|
||||||
|
loginPage.open();
|
||||||
|
|
||||||
|
loginPage.clickSocial("dummy");
|
||||||
|
|
||||||
|
driver.findElement(By.id("cancel")).click();
|
||||||
|
|
||||||
|
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
|
||||||
|
|
||||||
|
AccessTokenResponse response = oauth.doAccessTokenRequest(oauth.getCurrentQuery().get("code"), "password");
|
||||||
|
|
||||||
|
AccessToken token = oauth.verifyToken(response.getAccessToken());
|
||||||
|
Assert.assertEquals(36, token.getSubject().length());
|
||||||
|
|
||||||
|
UserRepresentation profile = keycloakRule.getUserById("test", token.getSubject());
|
||||||
|
Assert.assertEquals(36, profile.getUsername().length());
|
||||||
|
|
||||||
|
Assert.assertEquals("Bob", profile.getFirstName());
|
||||||
|
Assert.assertEquals("Builder", profile.getLastName());
|
||||||
|
Assert.assertEquals("bob@builder.com", profile.getEmail());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void profileUpdateRequired() {
|
public void profileUpdateRequired() {
|
||||||
keycloakRule.configure(new KeycloakSetup() {
|
keycloakRule.configure(new KeycloakSetup() {
|
||||||
|
@ -132,6 +157,7 @@ public class SocialLoginTest {
|
||||||
|
|
||||||
loginPage.clickSocial("dummy");
|
loginPage.clickSocial("dummy");
|
||||||
|
|
||||||
|
driver.findElement(By.id("id")).sendKeys("2");
|
||||||
driver.findElement(By.id("username")).sendKeys("dummy-user2");
|
driver.findElement(By.id("username")).sendKeys("dummy-user2");
|
||||||
driver.findElement(By.id("firstname")).sendKeys("Bob");
|
driver.findElement(By.id("firstname")).sendKeys("Bob");
|
||||||
driver.findElement(By.id("lastname")).sendKeys("Builder");
|
driver.findElement(By.id("lastname")).sendKeys("Builder");
|
||||||
|
|
|
@ -97,7 +97,7 @@ public class CreateUsersWorker implements Worker {
|
||||||
+ " which is too big.");
|
+ " which is too big.");
|
||||||
}
|
}
|
||||||
|
|
||||||
SocialLinkModel socialLink = new SocialLinkModel(socialProvider, username);
|
SocialLinkModel socialLink = new SocialLinkModel(socialProvider, username, username);
|
||||||
realm.addSocialLink(user, socialLink);
|
realm.addSocialLink(user, socialLink);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ public class ReadUsersWorker implements Worker {
|
||||||
|
|
||||||
// Try to search by social links
|
// Try to search by social links
|
||||||
if (searchBySocialLinks) {
|
if (searchBySocialLinks) {
|
||||||
SocialLinkModel socialLink = new SocialLinkModel("facebook", username);
|
SocialLinkModel socialLink = new SocialLinkModel("facebook", username, username);
|
||||||
realm.getUserBySocialLink(socialLink);
|
realm.getUserBySocialLink(socialLink);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue