use of com.serotonin.m2m2.vo.OAuth2LinkedAccount in project ma-core-public by infiniteautomation.
the class UserDao method linkAccount.
public void linkAccount(int userId, LinkedAccount account) {
if (account instanceof OAuth2LinkedAccount) {
OAuth2LinkedAccount oAuth2Account = (OAuth2LinkedAccount) account;
create.insertInto(oauth).set(oauth.userId, userId).set(oauth.issuer, oAuth2Account.getIssuer()).set(oauth.subject, oAuth2Account.getSubject()).execute();
} else {
throw new UnsupportedOperationException();
}
}
use of com.serotonin.m2m2.vo.OAuth2LinkedAccount in project ma-core-public by infiniteautomation.
the class UserDao method updateLinkedAccounts.
public void updateLinkedAccounts(int userId, Iterable<? extends LinkedAccount> accounts) {
this.doInTransaction(txStatus -> {
create.deleteFrom(oauth).where(oauth.userId.equal(userId)).execute();
InsertValuesStep3<OAuth2UsersRecord, Integer, String, String> insert = create.insertInto(oauth, oauth.userId, oauth.issuer, oauth.subject);
for (LinkedAccount account : accounts) {
if (account instanceof OAuth2LinkedAccount) {
OAuth2LinkedAccount oAuth2Account = (OAuth2LinkedAccount) account;
insert = insert.values(userId, oAuth2Account.getIssuer(), oAuth2Account.getSubject());
} else {
throw new UnsupportedOperationException();
}
}
insert.execute();
});
}
use of com.serotonin.m2m2.vo.OAuth2LinkedAccount in project ma-core-public by infiniteautomation.
the class DefaultUserMapper method mapUser.
@Override
public User mapUser(OAuth2UserRequest userRequest, OAuth2User oAuth2User) {
if (log.isDebugEnabled()) {
log.debug("Syncing OAuth2 user {} to Mango user", oAuth2User);
}
ClientRegistration clientRegistration = userRequest.getClientRegistration();
StandardClaimAccessor accessor = toAccessor(oAuth2User);
String registrationId = clientRegistration.getRegistrationId();
EnvironmentPropertyMapper userMapping = mapperFactory.forRegistrationId(registrationId, "userMapping.");
Optional<String> issuerOptional = userMapping.map("issuer.fixed");
if (!issuerOptional.isPresent()) {
issuerOptional = userMapping.map("issuer", accessor::getClaimAsString);
}
String issuer = issuerOptional.orElseThrow(() -> new IllegalStateException("Issuer is required"));
String subject = userMapping.map("subject", accessor::getClaimAsString).orElseThrow(() -> new IllegalStateException("Subject is required"));
LinkedAccount linkedAccount = new OAuth2LinkedAccount(issuer, subject);
User user = usersService.getUserForLinkedAccount(linkedAccount).orElseGet(() -> {
// only synchronize the username when creating the user
String usernamePrefix = userMapping.map("username.prefix").orElse("");
String usernameSuffix = userMapping.map("username.suffix").orElse("");
String username = userMapping.map("username", accessor::getClaimAsString).map(un -> usernamePrefix + un + usernameSuffix).orElse(// user will get a random XID for a username if claim is missing
null);
User newUser = new User();
newUser.setUsername(username);
newUser.setPassword(LOCKED_PASSWORD);
// in case role sync is not turned on
newUser.setRoles(Collections.singleton(PermissionHolder.USER_ROLE));
return newUser;
});
String emailPrefix = userMapping.map("email.prefix").orElse("");
String emailSuffix = userMapping.map("email.suffix").orElse("");
String email = userMapping.map("email", accessor::getClaimAsString).map(e -> emailPrefix + e + emailSuffix).orElse(// validation will fail if email is not set
null);
user.setEmail(email);
userMapping.map("name", accessor::getClaimAsString).ifPresent(user::setName);
userMapping.map("phone", accessor::getClaimAsString).ifPresent(user::setPhone);
userMapping.map("locale", accessor::getClaimAsString).ifPresent(user::setLocale);
userMapping.map("timezone", accessor::getClaimAsString).ifPresent(user::setTimezone);
if (userMapping.map("oauth2.client.default.userMapping.roles.sync", Boolean.class).orElse(true)) {
String rolePrefix = userMapping.map("roles.prefix").orElse("");
String roleSuffix = userMapping.map("roles.suffix").orElse("");
Set<String> ignoreRoles = Arrays.stream(userMapping.map("roles.ignore", String[].class).orElse(new String[0])).collect(Collectors.toSet());
Stream<String> oauthRoles = userMapping.map("roles", accessor::getClaimAsStringList).orElseGet(ArrayList::new).stream().filter(r -> !ignoreRoles.contains(r)).map(r -> userMapping.map("roles.map." + r).orElse(rolePrefix + r + roleSuffix));
Stream<String> addRoles = Arrays.stream(userMapping.map("roles.add", String[].class).orElse(new String[0]));
Set<Role> roles = Stream.concat(oauthRoles, addRoles).map(roleService::getOrInsert).map(RoleVO::getRole).collect(Collectors.toCollection(HashSet::new));
// ensure user role is present
roles.add(PermissionHolder.USER_ROLE);
user.setRoles(roles);
}
if (user.isNew()) {
usersService.insertUserForLinkedAccount(user, linkedAccount);
} else {
usersService.update(user.getId(), user);
}
return user;
}
Aggregations