Skip to content

Commit b894fff

Browse files
authored
Merge pull request #117 from auth0/add-email-endpoints
Add email-templates endpoints
2 parents 6eac172 + 26f0cea commit b894fff

8 files changed

Lines changed: 498 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ The Management API is divided into different entities. Each of them have the lis
279279
* **User Blocks:** See [Docs](https://auth0.com/docs/api/management/v2#!/User_Blocks/get_user_blocks). Access the methods by calling `mgmt.userBlocks()`.
280280
* **Users:** See [this](https://auth0.com/docs/api/management/v2#!/Users/get_users) and [this](https://auth0.com/docs/api/management/v2#!/Users_By_Email) doc. Access the methods by calling `mgmt.users()`.
281281
* **Blacklists:** See [Docs](https://auth0.com/docs/api/management/v2#!/Blacklists/get_tokens). Access the methods by calling `mgmt.blacklists()`.
282-
* **Emails:** See [Docs](https://auth0.com/docs/api/management/v2#!/Emails/get_provider). Access the methods by calling `mgmt.emailProvider()`.
282+
* **Email Providers:** See [Docs](https://auth0.com/docs/api/management/v2#!/Emails/get_provider). Access the methods by calling `mgmt.emailProvider()`.
283+
* **Email Templates:** See [Docs](https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName). Access the methods by calling `mgmt.emailTemplates()`.
283284
* **Guardian:** See [Docs](https://auth0.com/docs/api/management/v2#!/Guardian/get_factors). Access the methods by calling `mgmt.guardian()`.
284285
* **Stats:** See [Docs](https://auth0.com/docs/api/management/v2#!/Stats/get_active_users). Access the methods by calling `mgmt.stats()`.
285286
* **Tenants:** See [Docs](https://auth0.com/docs/api/management/v2#!/Tenants/get_settings). Access the methods by calling `mgmt.tenants()`.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.auth0.client.mgmt;
2+
3+
import com.auth0.json.mgmt.EmailTemplate;
4+
import com.auth0.net.CustomRequest;
5+
import com.auth0.net.Request;
6+
import com.auth0.utils.Asserts;
7+
import com.fasterxml.jackson.core.type.TypeReference;
8+
import okhttp3.HttpUrl;
9+
import okhttp3.OkHttpClient;
10+
11+
/**
12+
* Class that provides an implementation of the Email Templates methods of the Management API as defined in https://auth0.com/docs/api/management/v2#!/Email_Templates
13+
*/
14+
@SuppressWarnings({"WeakerAccess", "unused"})
15+
public class EmailTemplatesEntity extends BaseManagementEntity {
16+
17+
public static final String TEMPLATE_VERIFY_EMAIL = "verify_email";
18+
public static final String TEMPLATE_RESET_EMAIL = "reset_email";
19+
public static final String TEMPLATE_WELCOME_EMAIL = "welcome_email";
20+
public static final String TEMPLATE_BLOCKED_ACCOUNT = "blocked_account";
21+
public static final String TEMPLATE_STOLEN_CREDENTIALS = "stolen_credentials";
22+
public static final String TEMPLATE_ENROLLMENT_EMAIL = "enrollment_email";
23+
public static final String TEMPLATE_CHANGE_PASSWORD = "change_password";
24+
public static final String TEMPLATE_PASSWORD_RESET = "password_reset";
25+
public static final String TEMPLATE_MFA_OOB_CODE = "mfa_oob_code";
26+
27+
EmailTemplatesEntity(OkHttpClient client, HttpUrl baseUrl, String apiToken) {
28+
super(client, baseUrl, apiToken);
29+
}
30+
31+
/**
32+
* Request the Email Templates. A token with scope read:email_templates is needed.
33+
* See https://auth0.com/docs/api/management/v2#!/Email_Templates/get_email_templates_by_templateName
34+
*
35+
* @param templateName the template name to request. You can use any of the constants defined in {@link EmailTemplatesEntity}
36+
* @return a Request to execute.
37+
*/
38+
public Request<EmailTemplate> get(String templateName) {
39+
Asserts.assertNotNull(templateName, "template name");
40+
HttpUrl.Builder builder = baseUrl
41+
.newBuilder()
42+
.addPathSegments("api/v2/email-templates")
43+
.addPathSegment(templateName);
44+
String url = builder.build().toString();
45+
CustomRequest<EmailTemplate> request = new CustomRequest<>(client, url, "GET", new TypeReference<EmailTemplate>() {
46+
});
47+
request.addHeader("Authorization", "Bearer " + apiToken);
48+
return request;
49+
}
50+
51+
/**
52+
* Create an Email Template. A token with scope create:email_templates is needed.
53+
* See https://auth0.com/docs/api/management/v2#!/Email_Templates/post_email_templates
54+
*
55+
* @param template the template data to set
56+
* @return a Request to execute.
57+
*/
58+
public Request<EmailTemplate> create(EmailTemplate template) {
59+
Asserts.assertNotNull(template, "template");
60+
61+
String url = baseUrl
62+
.newBuilder()
63+
.addPathSegments("api/v2/email-templates")
64+
.build()
65+
.toString();
66+
CustomRequest<EmailTemplate> request = new CustomRequest<>(this.client, url, "POST", new TypeReference<EmailTemplate>() {
67+
});
68+
request.addHeader("Authorization", "Bearer " + apiToken);
69+
request.setBody(template);
70+
return request;
71+
}
72+
73+
/**
74+
* Patches the existing Email Template. A token with scope update:email_templates is needed.
75+
* See https://auth0.com/docs/api/management/v2#!/Email_Templates/patch_email_templates_by_templateName
76+
*
77+
* @param templateName the name of the template to update. You can use any of the constants defined in {@link EmailTemplatesEntity}
78+
* @param template the email template data to set.
79+
* @return a Request to execute.
80+
*/
81+
public Request<EmailTemplate> update(String templateName, EmailTemplate template) {
82+
Asserts.assertNotNull(templateName, "template name");
83+
Asserts.assertNotNull(template, "template");
84+
85+
String url = baseUrl
86+
.newBuilder()
87+
.addPathSegments("api/v2/email-templates")
88+
.addPathSegment(templateName)
89+
.build()
90+
.toString();
91+
CustomRequest<EmailTemplate> request = new CustomRequest<>(this.client, url, "PATCH", new TypeReference<EmailTemplate>() {
92+
});
93+
request.addHeader("Authorization", "Bearer " + apiToken);
94+
request.setBody(template);
95+
return request;
96+
}
97+
}

src/main/java/com/auth0/client/mgmt/ManagementAPI.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ public BlacklistsEntity blacklists() {
169169
return new BlacklistsEntity(client, baseUrl, apiToken);
170170
}
171171

172+
/**
173+
* Getter for the Email Templates entity.
174+
*
175+
* @return the Email Templates entity.
176+
*/
177+
public EmailTemplatesEntity emailTemplates() {
178+
return new EmailTemplatesEntity(client, baseUrl, apiToken);
179+
}
180+
172181
/**
173182
* Getter for the Email Provider entity.
174183
*
@@ -219,7 +228,7 @@ public TicketsEntity tickets() {
219228
*
220229
* @return the Resource Servers entity.
221230
*/
222-
public ResourceServerEntity resourceServers(){
231+
public ResourceServerEntity resourceServers() {
223232
return new ResourceServerEntity(client, baseUrl, apiToken);
224233
}
225234
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package com.auth0.json.mgmt;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
/**
8+
* Class that represents an Email Template object. Related to the {@link com.auth0.client.mgmt.EmailTemplatesEntity} entity.
9+
*/
10+
@SuppressWarnings({"unused", "WeakerAccess"})
11+
@JsonIgnoreProperties(ignoreUnknown = true)
12+
@JsonInclude(JsonInclude.Include.NON_NULL)
13+
public class EmailTemplate {
14+
15+
@JsonProperty("template")
16+
private String name;
17+
@JsonProperty("body")
18+
private String body;
19+
@JsonProperty("from")
20+
private String from;
21+
@JsonProperty("resultUrl")
22+
private String resultUrl;
23+
@JsonProperty("subject")
24+
private String subject;
25+
@JsonProperty("syntax")
26+
private String syntax;
27+
@JsonProperty("urlLifetimeInSeconds")
28+
private Integer urlLifetimeInSeconds;
29+
@JsonProperty("enabled")
30+
private Boolean enabled;
31+
32+
public EmailTemplate() {
33+
//Only here to set the syntax default value
34+
this.syntax = "liquid";
35+
}
36+
37+
/**
38+
* Getter for the name of the template.
39+
*
40+
* @return the name.
41+
*/
42+
@JsonProperty("template")
43+
public String getName() {
44+
return name;
45+
}
46+
47+
/**
48+
* Sets the name of the template.
49+
*/
50+
@JsonProperty("template")
51+
public void setName(String name) {
52+
this.name = name;
53+
}
54+
55+
/**
56+
* Getter for the template code.
57+
*
58+
* @return the template code.
59+
*/
60+
@JsonProperty("body")
61+
public String getBody() {
62+
return body;
63+
}
64+
65+
/**
66+
* Sets the template code
67+
*
68+
* @param body the code this template will have
69+
*/
70+
@JsonProperty("body")
71+
public void setBody(String body) {
72+
this.body = body;
73+
}
74+
75+
/**
76+
* Getter for the sender of the email
77+
*
78+
* @return the sender of the email
79+
*/
80+
@JsonProperty("from")
81+
public String getFrom() {
82+
return from;
83+
}
84+
85+
/**
86+
* Sets the sender of the email
87+
*
88+
* @param from the sender of the email
89+
*/
90+
@JsonProperty("from")
91+
public void setFrom(String from) {
92+
this.from = from;
93+
}
94+
95+
/**
96+
* Getter the URL to redirect the user to after a successful action.
97+
*
98+
* @return the URL to redirect the user to after a successful action.
99+
*/
100+
@JsonProperty("resultUrl")
101+
public String getResultUrl() {
102+
return resultUrl;
103+
}
104+
105+
/**
106+
* Sets the URL to redirect the user to after a successful action.
107+
*
108+
* @param resultUrl the URL to redirect the user to after a successful action.
109+
*/
110+
@JsonProperty("resultUrl")
111+
public void setResultUrl(String resultUrl) {
112+
this.resultUrl = resultUrl;
113+
}
114+
115+
/**
116+
* Getter for the subject of the email.
117+
*
118+
* @return the subject of the email.
119+
*/
120+
@JsonProperty("subject")
121+
public String getSubject() {
122+
return subject;
123+
}
124+
125+
/**
126+
* Sets the subject of the email.
127+
*
128+
* @param subject the subject of the email.
129+
*/
130+
@JsonProperty("subject")
131+
public void setSubject(String subject) {
132+
this.subject = subject;
133+
}
134+
135+
/**
136+
* Getter for the syntax used in the template's code.
137+
*
138+
* @return the syntax used in the template's code.
139+
*/
140+
@JsonProperty("syntax")
141+
public String getSyntax() {
142+
return syntax;
143+
}
144+
145+
/**
146+
* Sets for the syntax to be used in the template's code.
147+
* Default value is 'liquid'
148+
*
149+
* @param syntax the syntax to be used in the template's code.
150+
*/
151+
@JsonProperty("syntax")
152+
public void setSyntax(String syntax) {
153+
this.syntax = syntax;
154+
}
155+
156+
/**
157+
* Getter for the lifetime in seconds that the link within the email will be valid for.
158+
*
159+
* @return the lifetime in seconds that the link within the email will be valid for.
160+
*/
161+
@JsonProperty("urlLifetimeInSeconds")
162+
public Integer getUrlLifetimeInSeconds() {
163+
return urlLifetimeInSeconds;
164+
}
165+
166+
/**
167+
* Sets the lifetime in seconds that the link within the email will be valid for.
168+
*
169+
* @param urlLifetimeInSeconds the lifetime in seconds that the link within the email will be valid for.
170+
*/
171+
@JsonProperty("urlLifetimeInSeconds")
172+
public void setUrlLifetimeInSeconds(Integer urlLifetimeInSeconds) {
173+
this.urlLifetimeInSeconds = urlLifetimeInSeconds;
174+
}
175+
176+
/**
177+
* Whether or not this template is enabled.
178+
*
179+
* @return true if this template is enabled, false otherwise.
180+
*/
181+
@JsonProperty("enabled")
182+
public Boolean isEnabled() {
183+
return enabled;
184+
}
185+
186+
/**
187+
* Enables or disables this template.
188+
*
189+
* @param enabled whether this template is enabled or not.
190+
*/
191+
@JsonProperty("enabled")
192+
public void setEnabled(Boolean enabled) {
193+
this.enabled = enabled;
194+
}
195+
196+
}

src/test/java/com/auth0/client/MockServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class MockServer {
4545
public static final String MGMT_USER_BLOCKS = "src/test/resources/mgmt/user_blocks.json";
4646
public static final String MGMT_BLACKLISTED_TOKENS_LIST = "src/test/resources/mgmt/blacklisted_tokens_list.json";
4747
public static final String MGMT_EMAIL_PROVIDER = "src/test/resources/mgmt/email_provider.json";
48+
public static final String MGMT_EMAIL_TEMPLATE = "src/test/resources/mgmt/email_template.json";
4849
public static final String MGMT_USERS_LIST = "src/test/resources/mgmt/users_list.json";
4950
public static final String MGMT_USERS_PAGED_LIST = "src/test/resources/mgmt/users_paged_list.json";
5051
public static final String MGMT_USER = "src/test/resources/mgmt/user.json";

0 commit comments

Comments
 (0)