-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CLOUDTRUST-2296] Add a method on account API to send execute actions email #26
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,29 +3,43 @@ | |
import org.jboss.resteasy.annotations.cache.NoCache; | ||
import org.jboss.resteasy.spi.HttpRequest; | ||
import org.jboss.resteasy.spi.HttpResponse; | ||
import org.keycloak.authentication.actiontoken.execactions.ExecuteActionsActionToken; | ||
import org.keycloak.common.Profile; | ||
import org.keycloak.common.util.Time; | ||
import org.keycloak.email.EmailException; | ||
import org.keycloak.email.EmailTemplateProvider; | ||
import org.keycloak.events.EventBuilder; | ||
import org.keycloak.events.EventType; | ||
import org.keycloak.models.*; | ||
import org.keycloak.services.ErrorResponse; | ||
import org.keycloak.services.ServicesLogger; | ||
import org.keycloak.services.managers.Auth; | ||
import org.keycloak.services.resources.Cors; | ||
import org.keycloak.services.resources.LoginActionsService; | ||
import org.keycloak.services.resources.account.AccountRestService; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.NotFoundException; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.Response.Status; | ||
import javax.ws.rs.core.UriBuilder; | ||
|
||
/** | ||
* This is a copy of org.keycloak.services.resources.account.AccountRestService that fixes a bug with CORS | ||
* The inspiration for the fix came from org.keycloak.services.resources.admin.AdminRoot. | ||
*/ | ||
public class FixedAccountRestService extends AccountRestService { | ||
|
||
private KeycloakSession session; | ||
private Auth auth; | ||
private EventBuilder event; | ||
|
@@ -86,4 +100,53 @@ public Response deleteAccount() { | |
} | ||
} | ||
|
||
/** | ||
* Send execute actions email | ||
* @param lifespan | ||
* @param actions | ||
* @return | ||
*/ | ||
@Path("execute-actions-email") | ||
@PUT | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public Response executeActionsEmail(@QueryParam("lifespan") Integer lifespan, List<String> actions) { | ||
if (user.getEmail()==null) { | ||
return ErrorResponse.error("User email missing", Status.BAD_REQUEST); | ||
} | ||
|
||
if (!user.isEnabled()) { | ||
throw new WebApplicationException( | ||
ErrorResponse.error("User is disabled", Status.BAD_REQUEST)); | ||
} | ||
|
||
if (lifespan == null) { | ||
lifespan = realm.getActionTokenGeneratedByAdminLifespan(); | ||
} | ||
int expiration = Time.currentTime() + lifespan; | ||
String redirectUri = null; | ||
String clientId = Constants.ACCOUNT_MANAGEMENT_CLIENT_ID; | ||
ExecuteActionsActionToken token = new ExecuteActionsActionToken(user.getId(), expiration, actions, redirectUri, clientId); | ||
|
||
try { | ||
UriBuilder builder = LoginActionsService.actionTokenProcessor(session.getContext().getUri()); | ||
builder.queryParam("key", token.serialize(session, realm, session.getContext().getUri())); | ||
|
||
String link = builder.build(realm.getName()).toString(); | ||
|
||
this.session.getProvider(EmailTemplateProvider.class) | ||
.setAttribute(Constants.TEMPLATE_ATTR_REQUIRED_ACTIONS, token.getRequiredActions()) | ||
.setRealm(realm) | ||
.setUser(user) | ||
.sendExecuteActions(link, TimeUnit.SECONDS.toMinutes(lifespan)); | ||
|
||
//audit.user(user).detail(Details.EMAIL, user.getEmail()).detail(Details.CODE_ID, accessCode.getCodeId()).success(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To remove and/or convert into standard event |
||
|
||
//adminEvent.operation(OperationType.ACTION).resourcePath(session.getContext().getUri()).success(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To remove ? |
||
|
||
return Response.ok().build(); | ||
} catch (EmailException e) { | ||
ServicesLogger.LOGGER.failedToSendActionsEmail(e); | ||
return ErrorResponse.error("Failed to send execute actions email", Status.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand where does this Constants come from ... I don't see it in imports neither in package. What am I missing ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It comes from org.keycloak.models
(the code to send the mail is mainly copied from Keycloak)