Skip to content

Commit 0736c0a

Browse files
authored
Merge pull request #449 from gotify/appid
Authenticate with client token for pushing messages on gotify 3.0
2 parents bc3a4d6 + 05c885e commit 0736c0a

16 files changed

Lines changed: 750 additions & 53 deletions

File tree

app/src/main/kotlin/com/github/gotify/sharing/ShareActivity.kt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.github.gotify.api.ApiException
1616
import com.github.gotify.api.ClientFactory
1717
import com.github.gotify.client.api.MessageApi
1818
import com.github.gotify.client.model.Application
19-
import com.github.gotify.client.model.Message
19+
import com.github.gotify.client.model.CreateMessage
2020
import com.github.gotify.databinding.ActivityShareBinding
2121
import com.github.gotify.messages.provider.ApplicationHolder
2222
import kotlinx.coroutines.Dispatchers
@@ -107,15 +107,15 @@ internal class ShareActivity : AppCompatActivity() {
107107
return
108108
}
109109

110-
val message = Message()
110+
val message = CreateMessage()
111111
if (titleText.isNotEmpty()) {
112112
message.title = titleText
113113
}
114114
message.message = contentText
115115
message.priority = priority.toLong()
116116

117117
launchCoroutine {
118-
val response = executeMessageCall(appIndex, message)
118+
val response = executeMessageCall(appsHolder.get()[appIndex], message)
119119
withContext(Dispatchers.Main) {
120120
if (response) {
121121
Toast.makeText(this@ShareActivity, "Pushed!", Toast.LENGTH_LONG).show()
@@ -131,10 +131,16 @@ internal class ShareActivity : AppCompatActivity() {
131131
}
132132
}
133133

134-
private fun executeMessageCall(appIndex: Int, message: Message): Boolean {
135-
val pushClient = ClientFactory.clientToken(settings, appsHolder.get()[appIndex].token)
134+
private fun executeMessageCall(app: Application, message: CreateMessage): Boolean {
135+
// In gotify 3.0, tokens aren't returned in the API anymore, but the push api allows setting the appid with client auth.
136+
val client = if (app.token == null) {
137+
message.appid = app.id
138+
ClientFactory.clientToken(settings)
139+
} else {
140+
ClientFactory.clientToken(settings, app.token)
141+
}
136142
return try {
137-
val messageApi = pushClient.createService(MessageApi::class.java)
143+
val messageApi = client.createService(MessageApi::class.java)
138144
Api.execute(messageApi.createMessage(message))
139145
true
140146
} catch (apiException: ApiException) {

client/.openapi-generator/FILES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ src/main/java/com/github/gotify/client/model/Application.java
2525
src/main/java/com/github/gotify/client/model/ApplicationParams.java
2626
src/main/java/com/github/gotify/client/model/Client.java
2727
src/main/java/com/github/gotify/client/model/ClientParams.java
28+
src/main/java/com/github/gotify/client/model/CreateMessage.java
2829
src/main/java/com/github/gotify/client/model/CreateUserExternal.java
2930
src/main/java/com/github/gotify/client/model/CurrentUser.java
3031
src/main/java/com/github/gotify/client/model/ElevateRequest.java
@@ -39,6 +40,9 @@ src/main/java/com/github/gotify/client/model/OIDCExternalTokenResponse.java
3940
src/main/java/com/github/gotify/client/model/PagedMessages.java
4041
src/main/java/com/github/gotify/client/model/Paging.java
4142
src/main/java/com/github/gotify/client/model/PluginConf.java
43+
src/main/java/com/github/gotify/client/model/RegenerateTokenResponse.java
44+
src/main/java/com/github/gotify/client/model/SecurityUpdateAction.java
45+
src/main/java/com/github/gotify/client/model/SecurityUpdateActionResponse.java
4246
src/main/java/com/github/gotify/client/model/UpdateUserExternal.java
4347
src/main/java/com/github/gotify/client/model/User.java
4448
src/main/java/com/github/gotify/client/model/UserPass.java

client/src/main/java/com/github/gotify/client/api/ApplicationApi.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import com.github.gotify.client.model.ApplicationParams;
1414
import com.github.gotify.client.model.Error;
1515
import java.io.File;
16+
import com.github.gotify.client.model.SecurityUpdateAction;
17+
import com.github.gotify.client.model.SecurityUpdateActionResponse;
1618

1719
import java.util.ArrayList;
1820
import java.util.HashMap;
@@ -66,6 +68,21 @@ Call<Void> removeAppImage(
6668
@retrofit2.http.Path("id") Long id
6769
);
6870

71+
/**
72+
* Perform security updates on an application.
73+
* Requires elevated authentication.
74+
* @param id the application id (required)
75+
* @param body security update action descriptor (required)
76+
* @return Call&lt;SecurityUpdateActionResponse&gt;
77+
*/
78+
@Headers({
79+
"Content-Type:application/json"
80+
})
81+
@PUT("application/{id}/security")
82+
Call<SecurityUpdateActionResponse> updateAppSecurity(
83+
@retrofit2.http.Path("id") Long id, @retrofit2.http.Body SecurityUpdateAction body
84+
);
85+
6986
/**
7087
* Update an application.
7188
*

client/src/main/java/com/github/gotify/client/api/ClientApi.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ Call<Void> deleteClient(
4949
/**
5050
* Elevate a client session.
5151
* Requires elevated authentication.
52+
* @param id the client id (required)
5253
* @param body the elevation request (required)
5354
* @return Call&lt;Void&gt;
5455
*/
5556
@Headers({
5657
"Content-Type:application/json"
5758
})
58-
@POST("client:elevate")
59+
@POST("client/{id}/elevate")
5960
Call<Void> elevateClient(
60-
@retrofit2.http.Body ElevateRequest body
61+
@retrofit2.http.Path("id") Long id, @retrofit2.http.Body ElevateRequest body
6162
);
6263

6364
/**

client/src/main/java/com/github/gotify/client/api/MessageApi.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import okhttp3.ResponseBody;
1010
import okhttp3.MultipartBody;
1111

12+
import com.github.gotify.client.model.CreateMessage;
1213
import com.github.gotify.client.model.Error;
1314
import com.github.gotify.client.model.Message;
1415
import com.github.gotify.client.model.PagedMessages;
@@ -22,7 +23,7 @@
2223
public interface MessageApi {
2324
/**
2425
* Create a message.
25-
* __NOTE__: This API ONLY accepts an application token as authentication.
26+
* __NOTE__: When authenticating with a client token or basic auth, the request body must include \&quot;appid\&quot; referencing an application owned by the authenticated user. When authenticating with an application token, the application is derived from the token and any \&quot;appid\&quot; in the body is ignored.
2627
* @param body the message to add (required)
2728
* @return Call&lt;Message&gt;
2829
*/
@@ -31,7 +32,7 @@ public interface MessageApi {
3132
})
3233
@POST("message")
3334
Call<Message> createMessage(
34-
@retrofit2.http.Body Message body
35+
@retrofit2.http.Body CreateMessage body
3536
);
3637

3738
/**

client/src/main/java/com/github/gotify/client/model/Application.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
*/
2929
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.19.0")
3030
public class Application {
31+
public static final String SERIALIZED_NAME_CREATED_AT = "createdAt";
32+
@SerializedName(SERIALIZED_NAME_CREATED_AT)
33+
@javax.annotation.Nonnull
34+
private OffsetDateTime createdAt;
35+
3136
public static final String SERIALIZED_NAME_DEFAULT_PRIORITY = "defaultPriority";
3237
@SerializedName(SERIALIZED_NAME_DEFAULT_PRIORITY)
3338
@javax.annotation.Nullable
@@ -70,7 +75,7 @@ public class Application {
7075

7176
public static final String SERIALIZED_NAME_TOKEN = "token";
7277
@SerializedName(SERIALIZED_NAME_TOKEN)
73-
@javax.annotation.Nonnull
78+
@javax.annotation.Nullable
7479
private String token;
7580

7681
public Application() {
@@ -80,20 +85,34 @@ public Application() {
8085
*/
8186

8287
public Application(
88+
OffsetDateTime createdAt,
8389
Long id,
8490
String image,
8591
Boolean internal,
8692
OffsetDateTime lastUsed,
8793
String token
8894
) {
8995
this();
96+
this.createdAt = createdAt;
9097
this.id = id;
9198
this.image = image;
9299
this.internal = internal;
93100
this.lastUsed = lastUsed;
94101
this.token = token;
95102
}
96103

104+
/**
105+
* The date the application was created.
106+
* @return createdAt
107+
*/
108+
@javax.annotation.Nonnull
109+
110+
public OffsetDateTime getCreatedAt() {
111+
return createdAt;
112+
}
113+
114+
115+
97116
public Application defaultPriority(@javax.annotation.Nullable Long defaultPriority) {
98117

99118
this.defaultPriority = defaultPriority;
@@ -230,7 +249,7 @@ public void setSortKey(@javax.annotation.Nonnull String sortKey) {
230249
* The application token. Can be used as &#x60;appToken&#x60;. See Authentication.
231250
* @return token
232251
*/
233-
@javax.annotation.Nonnull
252+
@javax.annotation.Nullable
234253

235254
public String getToken() {
236255
return token;
@@ -247,7 +266,8 @@ public boolean equals(Object o) {
247266
return false;
248267
}
249268
Application application = (Application) o;
250-
return Objects.equals(this.defaultPriority, application.defaultPriority) &&
269+
return Objects.equals(this.createdAt, application.createdAt) &&
270+
Objects.equals(this.defaultPriority, application.defaultPriority) &&
251271
Objects.equals(this.description, application.description) &&
252272
Objects.equals(this.id, application.id) &&
253273
Objects.equals(this.image, application.image) &&
@@ -260,13 +280,14 @@ public boolean equals(Object o) {
260280

261281
@Override
262282
public int hashCode() {
263-
return Objects.hash(defaultPriority, description, id, image, internal, lastUsed, name, sortKey, token);
283+
return Objects.hash(createdAt, defaultPriority, description, id, image, internal, lastUsed, name, sortKey, token);
264284
}
265285

266286
@Override
267287
public String toString() {
268288
StringBuilder sb = new StringBuilder();
269289
sb.append("class Application {\n");
290+
sb.append(" createdAt: ").append(toIndentedString(createdAt)).append("\n");
270291
sb.append(" defaultPriority: ").append(toIndentedString(defaultPriority)).append("\n");
271292
sb.append(" description: ").append(toIndentedString(description)).append("\n");
272293
sb.append(" id: ").append(toIndentedString(id)).append("\n");

client/src/main/java/com/github/gotify/client/model/Client.java

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,26 @@
2828
*/
2929
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.19.0")
3030
public class Client {
31+
public static final String SERIALIZED_NAME_CREATED_AT = "createdAt";
32+
@SerializedName(SERIALIZED_NAME_CREATED_AT)
33+
@javax.annotation.Nonnull
34+
private OffsetDateTime createdAt;
35+
3136
public static final String SERIALIZED_NAME_ELEVATED_UNTIL = "elevatedUntil";
3237
@SerializedName(SERIALIZED_NAME_ELEVATED_UNTIL)
3338
@javax.annotation.Nullable
3439
private OffsetDateTime elevatedUntil;
3540

41+
public static final String SERIALIZED_NAME_EXPIRES_AFTER_INACTIVITY_SECONDS = "expiresAfterInactivitySeconds";
42+
@SerializedName(SERIALIZED_NAME_EXPIRES_AFTER_INACTIVITY_SECONDS)
43+
@javax.annotation.Nullable
44+
private Long expiresAfterInactivitySeconds;
45+
46+
public static final String SERIALIZED_NAME_EXPIRES_AT = "expiresAt";
47+
@SerializedName(SERIALIZED_NAME_EXPIRES_AT)
48+
@javax.annotation.Nullable
49+
private OffsetDateTime expiresAt;
50+
3651
public static final String SERIALIZED_NAME_ID = "id";
3752
@SerializedName(SERIALIZED_NAME_ID)
3853
@javax.annotation.Nonnull
@@ -50,7 +65,7 @@ public class Client {
5065

5166
public static final String SERIALIZED_NAME_TOKEN = "token";
5267
@SerializedName(SERIALIZED_NAME_TOKEN)
53-
@javax.annotation.Nonnull
68+
@javax.annotation.Nullable
5469
private String token;
5570

5671
public Client() {
@@ -60,18 +75,34 @@ public Client() {
6075
*/
6176

6277
public Client(
78+
OffsetDateTime createdAt,
6379
OffsetDateTime elevatedUntil,
80+
OffsetDateTime expiresAt,
6481
Long id,
6582
OffsetDateTime lastUsed,
6683
String token
6784
) {
6885
this();
86+
this.createdAt = createdAt;
6987
this.elevatedUntil = elevatedUntil;
88+
this.expiresAt = expiresAt;
7089
this.id = id;
7190
this.lastUsed = lastUsed;
7291
this.token = token;
7392
}
7493

94+
/**
95+
* The date the client was created.
96+
* @return createdAt
97+
*/
98+
@javax.annotation.Nonnull
99+
100+
public OffsetDateTime getCreatedAt() {
101+
return createdAt;
102+
}
103+
104+
105+
75106
/**
76107
* The time until which this client&#39;s session is elevated.
77108
* @return elevatedUntil
@@ -84,6 +115,39 @@ public OffsetDateTime getElevatedUntil() {
84115

85116

86117

118+
public Client expiresAfterInactivitySeconds(@javax.annotation.Nullable Long expiresAfterInactivitySeconds) {
119+
120+
this.expiresAfterInactivitySeconds = expiresAfterInactivitySeconds;
121+
return this;
122+
}
123+
124+
/**
125+
* The number of seconds of inactivity after which the client is removed. 0 means the client never expires.
126+
* @return expiresAfterInactivitySeconds
127+
*/
128+
@javax.annotation.Nullable
129+
130+
public Long getExpiresAfterInactivitySeconds() {
131+
return expiresAfterInactivitySeconds;
132+
}
133+
134+
135+
public void setExpiresAfterInactivitySeconds(@javax.annotation.Nullable Long expiresAfterInactivitySeconds) {
136+
this.expiresAfterInactivitySeconds = expiresAfterInactivitySeconds;
137+
}
138+
139+
/**
140+
* The time at which this client will expire due to inactivity, or null if it never expires.
141+
* @return expiresAt
142+
*/
143+
@javax.annotation.Nullable
144+
145+
public OffsetDateTime getExpiresAt() {
146+
return expiresAt;
147+
}
148+
149+
150+
87151
/**
88152
* The client id.
89153
* @return id
@@ -133,7 +197,7 @@ public void setName(@javax.annotation.Nonnull String name) {
133197
* The client token. Can be used as &#x60;clientToken&#x60;. See Authentication.
134198
* @return token
135199
*/
136-
@javax.annotation.Nonnull
200+
@javax.annotation.Nullable
137201

138202
public String getToken() {
139203
return token;
@@ -150,7 +214,10 @@ public boolean equals(Object o) {
150214
return false;
151215
}
152216
Client client = (Client) o;
153-
return Objects.equals(this.elevatedUntil, client.elevatedUntil) &&
217+
return Objects.equals(this.createdAt, client.createdAt) &&
218+
Objects.equals(this.elevatedUntil, client.elevatedUntil) &&
219+
Objects.equals(this.expiresAfterInactivitySeconds, client.expiresAfterInactivitySeconds) &&
220+
Objects.equals(this.expiresAt, client.expiresAt) &&
154221
Objects.equals(this.id, client.id) &&
155222
Objects.equals(this.lastUsed, client.lastUsed) &&
156223
Objects.equals(this.name, client.name) &&
@@ -159,14 +226,17 @@ public boolean equals(Object o) {
159226

160227
@Override
161228
public int hashCode() {
162-
return Objects.hash(elevatedUntil, id, lastUsed, name, token);
229+
return Objects.hash(createdAt, elevatedUntil, expiresAfterInactivitySeconds, expiresAt, id, lastUsed, name, token);
163230
}
164231

165232
@Override
166233
public String toString() {
167234
StringBuilder sb = new StringBuilder();
168235
sb.append("class Client {\n");
236+
sb.append(" createdAt: ").append(toIndentedString(createdAt)).append("\n");
169237
sb.append(" elevatedUntil: ").append(toIndentedString(elevatedUntil)).append("\n");
238+
sb.append(" expiresAfterInactivitySeconds: ").append(toIndentedString(expiresAfterInactivitySeconds)).append("\n");
239+
sb.append(" expiresAt: ").append(toIndentedString(expiresAt)).append("\n");
170240
sb.append(" id: ").append(toIndentedString(id)).append("\n");
171241
sb.append(" lastUsed: ").append(toIndentedString(lastUsed)).append("\n");
172242
sb.append(" name: ").append(toIndentedString(name)).append("\n");

0 commit comments

Comments
 (0)