Skip to content

Commit 6e13be3

Browse files
committed
add authentication tests
1 parent 29b14c3 commit 6e13be3

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

J4JMapLibrary/projections/google/GoogleMapsProjection.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace J4JSoftware.J4JMapLibrary;
2929
[ Projection( "GoogleMaps" ) ]
3030
public sealed class GoogleMapsProjection : StaticProjection
3131
{
32+
private bool _pendingInitializtion;
33+
3234
public GoogleMapsProjection(
3335
ILoggerFactory? loggerFactory = null
3436
)
@@ -92,17 +94,28 @@ protected override bool ValidateCredentials( object credentials )
9294

9395
Initialized = false;
9496

95-
ApiKey = ((GoogleCredentials) Credentials!).ApiKey;
96-
Signature = ((GoogleCredentials)Credentials).SignatureSecret;
97+
ApiKey = ( (GoogleCredentials) Credentials! ).ApiKey;
98+
Signature = ( (GoogleCredentials) Credentials ).SignatureSecret;
99+
100+
// the only way to check Google credentials is to try and retrieve a map image
101+
// sadly, that can fail for reasons other than invalid credentials
102+
// we also have to temporarily override the initialized check
103+
_pendingInitializtion = true;
104+
105+
var testBytes = await GetImageAsync( MapTile.CreateStaticMapTile( this, 0, 0, 0, LoggerFactory ), ctx );
106+
Initialized = testBytes != null;
107+
108+
_pendingInitializtion = false;
97109

98-
Initialized = true;
110+
if( !Initialized )
111+
Logger?.LogError( "Failed to retrieve map image, Google credentials may be invalid" );
99112

100-
return true;
113+
return Initialized;
101114
}
102115

103116
protected override HttpRequestMessage? CreateMessage( MapTile mapTile )
104117
{
105-
if( !Initialized )
118+
if( !_pendingInitializtion && !Initialized )
106119
{
107120
Logger?.LogError( "Trying to create image retrieval HttpRequestMessage when uninitialized" );
108121
return null;

J4JMapLibrary/projections/openmap/OpenStreetMapsProjection.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ protected override bool ValidateCredentials(object credentials)
6565

6666
UserAgent = ( (OpenStreetCredentials) Credentials! ).UserAgent;
6767

68-
Initialized = true;
68+
// user agent must be unique, but this is all we can do to check it
69+
Initialized = !string.IsNullOrEmpty( UserAgent );
70+
71+
if( !Initialized )
72+
Logger?.LogError("Empty or undefined user agent value");
73+
6974
return Initialized;
7075
}
7176

J4JMapLibrary/projections/openmap/OpenTopoMapsProjection.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ protected override bool ValidateCredentials(object credentials)
6666

6767
UserAgent = ( (OpenTopoCredentials) Credentials! ).UserAgent;
6868

69-
Initialized = true;
69+
// user agent must be unique, but this is all we can do to check it
70+
Initialized = !string.IsNullOrEmpty(UserAgent);
71+
72+
if (!Initialized)
73+
Logger?.LogError("Empty or undefined user agent value");
74+
7075
return Initialized;
7176
}
7277

0 commit comments

Comments
 (0)