Skip to content

Commit

Permalink
Merge pull request #22 from arnaudleclerc/releases/0.8.0
Browse files Browse the repository at this point in the history
Adding Anonymous authentication support
  • Loading branch information
arnaudleclerc authored Dec 8, 2020
2 parents 734b784 + 7b5b204 commit 75e83d4
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 8 deletions.
68 changes: 67 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ If you plan to use the drawing toolbar, you also need to include the following c

### Register the Components

You will need to pass the authentication information of your `AzureMaps` instance to the library. `SubscriptionKey` and `Aad` authentication are supported. You will need to call the `AddAzureMapsControl` method on your services.
You will need to pass the authentication information of your `AzureMaps` instance to the library. `SubscriptionKey`, `Aad` and `Anonymous` authentication are supported. You will need to call the `AddAzureMapsControl` method on your services.

You can authenticate using a `subscription key` :

Expand All @@ -45,6 +45,7 @@ You can authenticate using a `subscription key` :
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddAzureMapsControl(configuration => configuration.SubscriptionKey = "Your Subscription Key");
}
```
Expand All @@ -65,6 +66,71 @@ public void ConfigureServices(IServiceCollection services)
}
```

The `Anonymous` authentication requires only a `ClientId`:

```
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor(options => options.DetailedErrors = true);
services.AddAzureMapsControl(configuration => configuration.ClientId = Configuration["AzureMaps:ClientId"])
}
```

It also needs to fetch the token to send to the requests of the atlas library. For that, you have to override the `window.azureMapsControl.extensions.getTokenCallback` method on your application after referencing `azure-maps-control.js` and resolve the token in it. For example :

```
@page "/"
@namespace AzureMapsControl.Sample.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AzureMapsControl.Sample</title>
<base href="~/" />
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.css" type="text/css" />
<link rel="stylesheet" href="https://atlas.microsoft.com/sdk/javascript/drawing/0.1/atlas-drawing.min.css" type="text/css" />
<style>
body {
margin: 0;
}
#map {
position: absolute;
width: 100%;
min-width: 290px;
height: 100%;
}
</style>
</head>
<body>
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.js"></script>
<script src="https://atlas.microsoft.com/sdk/javascript/drawing/0.1/atlas-drawing.min.js"></script>
<script src="_content/AzureMapsControl.Components/azure-maps-control.js"></script>
<script src="_framework/blazor.server.js"></script>
<script type="text/javascript">
window.azureMapsControl.extensions.getTokenCallback = (resolve, reject, map) => {
const url = "url_of_my_token_endpoint";
fetch(url).then(function (response) {
return response.text();
}).then(function (token) {
resolve(token);
}); };
</script>
</body>
</html>
```

## How to use

- [Map](docs/map)
Expand Down
11 changes: 10 additions & 1 deletion samples/AzureMapsControl.Sample/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,19 @@
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>

<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.js"></script>
<script src="https://atlas.microsoft.com/sdk/javascript/drawing/0.1/atlas-drawing.min.js"></script>
<script src="_content/AzureMapsControl.Components/azure-maps-control.js"></script>
<script src="_framework/blazor.server.js"></script>
<script type="text/javascript">
window.azureMapsControl.extensions.getTokenCallback = (resolve, reject, map) => {
const url = "url_of_my_token_endpoint";
fetch(url).then(function (response) {
return response.text();
}).then(function (token) {
resolve(token);
});
};
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions samples/AzureMapsControl.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public void ConfigureServices(IServiceCollection services)
//This code uses a subscription key authentication
//services.AddAzureMapsControl(configuration => configuration.SubscriptionKey = Configuration["AzureMaps:SubscriptionKey"]);

//This code uses an anonymous authentication
//services.AddAzureMapsControl(configuration => configuration.ClientId = Configuration["AzureMaps:ClientId"]);

//This code uses an AAD authentication
services.AddAzureMapsControl(configuration => {
configuration.AadAppId = Configuration["AzureMaps:AadAppId"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public sealed class AzureMapsConfiguration
/// </summary>
public string SubscriptionKey { get; set; }

internal bool Validate() => !string.IsNullOrWhiteSpace(AuthType);

internal string AuthType
{
get {
Expand All @@ -41,11 +43,15 @@ internal string AuthType
return "subscriptionKey";
}

return !string.IsNullOrWhiteSpace(AadAppId)
if (!string.IsNullOrWhiteSpace(ClientId))
{
return !string.IsNullOrWhiteSpace(AadAppId)
&& !string.IsNullOrWhiteSpace(AadTenant)
&& !string.IsNullOrWhiteSpace(ClientId)
? "aad"
: "anonymous";
}

return null;
}
}
}
Expand All @@ -67,6 +73,10 @@ public override void Write(Utf8JsonWriter writer, AzureMapsConfiguration value,
writer.WriteString("aadTenant", value.AadTenant);
writer.WriteString("clientId", value.ClientId);
}
else if (value.AuthType == "anonymous")
{
writer.WriteString("clientId", value.ClientId);
}
writer.WriteEndObject();
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/AzureMapsControl.Components/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static IServiceCollection AddAzureMapsControl(this IServiceCollection ser
.AddSingleton<IMapAdderService>(sp => sp.GetRequiredService<MapService>())
.AddSingleton<IMapService>(sp => sp.GetRequiredService<MapService>())
.AddOptions<AzureMapsConfiguration>()
.Configure(configure);
.Configure(configure)
.Validate(configuration => configuration.Validate(), "The given AzureMapsConfiguration is invalid");

return services;
}
Expand Down
8 changes: 6 additions & 2 deletions src/AzureMapsControl.Components/wwwroot/azure-maps-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ window.azureMapsControl = {
});
} else {
atlas.setAuthenticationOptions({
authType: configuration.authType
authType: configuration.authType,
getToken: window.azureMapsControl.extensions.getTokenCallback
});
}

Expand Down Expand Up @@ -683,5 +684,8 @@ window.azureMapsControl = {
const result = properties || {};
result.type = type;
return result;
},
extensions: {
getTokenCallback: function (resolve, reject, map) { }
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void Should_HaveSubscriptionKeyAuthType()
};

Assert.Equal("subscriptionKey", configuration.AuthType);
Assert.True(configuration.Validate());
}

[Fact]
Expand All @@ -29,13 +30,25 @@ public void Should_HaveAddAuthType()
};

Assert.Equal("aad", configuration.AuthType);
Assert.True(configuration.Validate());
}

[Fact]
public void Should_HaveAnonymousAuthType()
{
var configuration = new AzureMapsConfiguration();
var configuration = new AzureMapsConfiguration {
ClientId = "clientId"
};
Assert.Equal("anonymous", configuration.AuthType);
Assert.True(configuration.Validate());
}

[Fact]
public void Should_NotHaveAnAuthType()
{
var configuration = new AzureMapsConfiguration();
Assert.Null(configuration.AuthType);
Assert.False(configuration.Validate());
}
}
}

0 comments on commit 75e83d4

Please sign in to comment.