Skip to content

Commit 169d0e0

Browse files
author
Łukasz Paczos
committed
do not serialize access tokens
Ensures that access tokens are not serialized in order to avoid often unintentional leaks of the data when paired with other tools and utilities that serialize and store either the whole route response, route, or only the options. Unfortunately, due the limitations of the code generation tools used we need to make the token field nullable until we find a better solution.
1 parent 9b01506 commit 169d0e0

7 files changed

Lines changed: 92 additions & 52 deletions

File tree

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/DirectionsResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public static DirectionsResponse fromJson(@NonNull String json) {
146146
* @return a new instance of this class defined by the values passed inside this static factory
147147
* method
148148
* @see RouteOptions#fromUrl(java.net.URL)
149-
* @see RouteOptions#fromJson(String)
149+
* @see RouteOptions#fromJson(String, String)
150150
*/
151151
public static DirectionsResponse fromJson(
152152
@NonNull String json, @Nullable RouteOptions routeOptions, @Nullable String requestUuid) {

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/DirectionsRoute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public static DirectionsRoute fromJson(@NonNull String json) {
193193
* @return a new instance of this class defined by the values passed inside this static factory
194194
* method
195195
* @see RouteOptions#fromUrl(java.net.URL)
196-
* @see RouteOptions#fromJson(String)
196+
* @see RouteOptions#fromJson(String, String)
197197
*/
198198
public static DirectionsRoute fromJson(
199199
@NonNull String json, @Nullable RouteOptions routeOptions, @Nullable String requestUuid

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/RouteOptions.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.mapbox.api.directions.v5.utils.FormatUtils;
1515
import com.mapbox.api.directions.v5.utils.ParseUtils;
1616
import com.mapbox.geojson.Point;
17+
import com.ryanharter.auto.value.gson.Ignore;
1718
import java.io.UnsupportedEncodingException;
1819
import java.net.URL;
1920
import java.net.URLDecoder;
@@ -366,11 +367,15 @@ public List<String> annotationsList() {
366367

367368
/**
368369
* A valid Mapbox access token used to making the request.
370+
* <p>
371+
* Avoiding to provide a token will most-likely result in a failure, however,
372+
* it's annotated as nullable to prevent serialization of tokens.
369373
*
370374
* @return a string representing the Mapbox access token
371375
*/
372376
@SerializedName("access_token")
373-
@NonNull
377+
@Ignore(Ignore.Type.SERIALIZATION)
378+
@Nullable
374379
public abstract String accessToken();
375380

376381
/**
@@ -670,14 +675,38 @@ public static TypeAdapter<RouteOptions> typeAdapter(Gson gson) {
670675

671676
/**
672677
* Create a new instance of this class by passing in a formatted valid JSON String.
678+
* <p>
679+
* The Mapbox Access Token that was part of the original object was not serialized and needs
680+
* to be provided again.
681+
* The options will not be valid for a request without a Mapbox Access Token.
682+
*
683+
* @param json a formatted valid JSON string defining a RouteOptions
684+
* @param accessToken a Mapbox Access Token
685+
* @return a new instance of this class defined by the values passed inside this static factory
686+
* method
687+
* @see #fromUrl(URL)
688+
*/
689+
@NonNull
690+
public static RouteOptions fromJson(@NonNull String json, @Nullable String accessToken) {
691+
return fromJson(json).toBuilder().accessToken(accessToken).build();
692+
}
693+
694+
/**
695+
* Create a new instance of this class by passing in a formatted valid JSON String.
696+
* <p>
697+
* The Mapbox Access Token that was part of the original object was not serialized and needs
698+
* to be provided again.
699+
* The options will not be valid for a request without a Mapbox Access Token so make sure to
700+
* provide a token with {@link #fromJson(String, String)}
701+
* or rebuild the options with {@link #toBuilder()}.
673702
*
674703
* @param json a formatted valid JSON string defining a RouteOptions
675704
* @return a new instance of this class defined by the values passed inside this static factory
676705
* method
677706
* @see #fromUrl(URL)
678707
*/
679708
@NonNull
680-
public static RouteOptions fromJson(String json) {
709+
public static RouteOptions fromJson(@NonNull String json) {
681710
GsonBuilder gson = new GsonBuilder();
682711
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
683712
return gson.create().fromJson(json, RouteOptions.class);
@@ -689,7 +718,7 @@ public static RouteOptions fromJson(String json) {
689718
* @param url request URL
690719
* @return a new instance of this class defined by the values passed inside this static factory
691720
* method
692-
* @see #fromJson(String)
721+
* @see #fromJson(String, String)
693722
*/
694723
@NonNull
695724
public static RouteOptions fromUrl(@NonNull URL url) {
@@ -1077,11 +1106,13 @@ public Builder annotationsList(@Nullable List<String> annotations) {
10771106
/**
10781107
* A valid Mapbox access token used to making the request.
10791108
*
1080-
* @param accessToken a string containing a valid Mapbox access token
1109+
* @param accessToken a string containing a valid Mapbox access token.
1110+
* Avoiding to provide a token will most-likely result in a failure, however,
1111+
* it's annotated as nullable to prevent serialization of tokens.
10811112
* @return this builder for chaining options together
10821113
*/
10831114
@NonNull
1084-
public abstract Builder accessToken(@NonNull String accessToken);
1115+
public abstract Builder accessToken(@Nullable String accessToken);
10851116

10861117
/**
10871118
* Exclude certain road types from routing. The default is to not exclude anything from the

0 commit comments

Comments
 (0)