-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed group timer add sending the repeat property incorrectly - Added add legacy device route (internal use only) - Some in progress work for events and oAuth 2 authentication
- Loading branch information
1 parent
0c0053f
commit f57d3ae
Showing
12 changed files
with
346 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\WifiPlug.Api\WifiPlug.Api.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using WifiPlug.Api; | ||
|
||
namespace Example.EventCli | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
//EventClient eventClient = new EventClient("ws://localhost", "devkey", "devsecret"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/WifiPlug.Api/Authentication/OAuth2Authentication.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace WifiPlug.Api.Authentication | ||
{ | ||
/// <summary> | ||
/// Provides full OAuth2 authentication. | ||
/// </summary> | ||
sealed class OAuth2Authentication : BearerAuthentication | ||
{ | ||
#region Constants | ||
internal const string TokenUrl = "https://account.wifiplug.co.uk/oauth2/token"; | ||
#endregion | ||
|
||
#region Fields | ||
private Uri _tokenUri; | ||
private string _refreshToken; | ||
#endregion | ||
|
||
#region Methods | ||
/// <summary> | ||
/// Applies bearer authentication to an outgoing request. | ||
/// </summary> | ||
/// <param name="request">The request.</param> | ||
public override void Apply(HttpRequestMessage request) { | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _bearerToken); | ||
} | ||
|
||
/// <summary> | ||
/// Deserializes the bearer authentication from a stream. | ||
/// </summary> | ||
/// <param name="stream">The stream.</param> | ||
public override void Deserialize(Stream stream) { | ||
// deserialize bearer token | ||
base.Deserialize(stream); | ||
|
||
// read refresh token | ||
BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, true); | ||
|
||
|
||
} | ||
|
||
/// <summary> | ||
/// Serializes the bearer authentication to a stream. | ||
/// </summary> | ||
/// <param name="stream">The stream.</param> | ||
public override void Serialize(Stream stream) { | ||
// serialize bearer token | ||
base.Serialize(stream); | ||
|
||
// write refresh token | ||
BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8, true); | ||
} | ||
|
||
/// <summary> | ||
/// Refreshes the token. | ||
/// </summary> | ||
/// <param name="client">The client.</param> | ||
/// <returns></returns> | ||
public override Task<bool> ReauthorizeAsync(ApiClient client) { | ||
// create refresh client | ||
HttpClient refreshClient = new HttpClient(); | ||
refreshClient.BaseAddress = _tokenUri; | ||
|
||
// make request | ||
//refreshClient.PostAsync("/") | ||
|
||
return Task.FromResult(false); | ||
} | ||
#endregion | ||
|
||
#region Entities | ||
class RefreshAccessTokenEntity | ||
{ | ||
[JsonProperty("grant_type")] | ||
public string GrantType { get; } = "refresh_token"; | ||
|
||
[JsonProperty("refresh_token")] | ||
public string RefreshToken { get; set; } | ||
} | ||
#endregion | ||
|
||
#region Constructors | ||
/// <summary> | ||
/// Creates an empty oAuth2 authentication object. | ||
/// </summary> | ||
public OAuth2Authentication() | ||
: base() { | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new oAuth2 authentication object from an access and refresh token. | ||
/// </summary> | ||
/// <param name="accessToken">The access token.</param> | ||
/// <param name="refreshToken">The refresh token.</param> | ||
public OAuth2Authentication(string accessToken, string refreshToken) | ||
: this(null, accessToken, refreshToken) { | ||
_refreshToken = refreshToken; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new oAuth2 authentication object from an access and refresh token. | ||
/// </summary> | ||
/// <param name="tokenUri">The optional token URI.</param> | ||
/// <param name="accessToken">The access token.</param> | ||
/// <param name="refreshToken">The refresh token.</param> | ||
public OAuth2Authentication(string tokenUri, string accessToken, string refreshToken) { | ||
_tokenUri = tokenUri == null ? new Uri(TokenUrl) : new Uri(tokenUri); | ||
_refreshToken = refreshToken; | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace WifiPlug.Api.Entities | ||
{ | ||
/// <summary> | ||
/// Represents a request to add a device, this is a legacy request and not supported by most API keys. | ||
/// </summary> | ||
public class DeviceAddEntity | ||
{ | ||
/// <summary> | ||
/// Gets or sets the name. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "name")] | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the MAC address. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "mac_address")] | ||
public string MacAddress { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the type code. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "type_code")] | ||
public int TypeCode { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the variant. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "variant")] | ||
public string Variant { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the firmware version. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "firmware_version")] | ||
public string FirmwareVersion { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace WifiPlug.Api | ||
{ | ||
/// <summary> | ||
/// Represents a received event. | ||
/// </summary> | ||
class Event | ||
{ | ||
#region Properties | ||
public string Name { get; private set; } | ||
public string ResourceType { get; private set; } | ||
public string Resource { get; private set; } | ||
public JObject Payload { get; private set; } | ||
#endregion | ||
|
||
#region Constructors | ||
|
||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.WebSockets; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace WifiPlug.Api | ||
{ | ||
/// <summary> | ||
/// Provides access to the WIFIPLUG event subscription API. | ||
/// </summary> | ||
class EventClient : IObservable<Event> | ||
{ | ||
#region Constants | ||
internal const string API_URL = "wss://event.wifiplug.co.uk/v1.0"; | ||
#endregion | ||
|
||
#region Fields | ||
private string _apiKey = null; | ||
private string _apiSecret = null; | ||
private ClientWebSocket _client = null; | ||
private Uri _uri = null; | ||
#endregion | ||
|
||
#region Methods | ||
/// <summary> | ||
/// Subscribes to the provided selector. | ||
/// </summary> | ||
/// <param name="selector">The subscription selector.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <example>await SubscribeAsync("device/*/*);</example> | ||
/// <returns></returns> | ||
public async Task SubscribeAsync(string selector, CancellationToken cancellationToken = default(CancellationToken)) { | ||
// connect if not open | ||
if (_client.State != WebSocketState.Open) | ||
await _client.ConnectAsync(_uri, cancellationToken).ConfigureAwait(false); | ||
|
||
// throw if cancelled | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
// subscribe | ||
//_client.SendAsync(new ArraySegment<byte>()); | ||
} | ||
|
||
/// <summary> | ||
/// Unsubscribes the provided selector, you can only unsubscribe the exact selector you subscribed previously. | ||
/// </summary> | ||
/// <param name="selector">The selector.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns></returns> | ||
public async Task UnsubscribeAsync(string selector, CancellationToken cancellationToken = default(CancellationToken)) { | ||
// connect if not open | ||
if (_client.State != WebSocketState.Open) | ||
await _client.ConnectAsync(_uri, cancellationToken).ConfigureAwait(false); | ||
|
||
// throw if cancelled | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
// unsubscribe | ||
} | ||
|
||
/// <summary> | ||
/// Closes the underlying event streaming client gracefully. | ||
/// </summary> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns></returns> | ||
public Task CloseAsync(CancellationToken cancellationToken = default(CancellationToken)) { | ||
return _client.CloseAsync(WebSocketCloseStatus.NormalClosure, "Client requested closure", cancellationToken); | ||
} | ||
#endregion | ||
|
||
public IDisposable Subscribe(IObserver<Event> observer) { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
#region Constructors | ||
/// <summary> | ||
/// Creates a new event client and configures the provided event URL and api credentials. | ||
/// </summary> | ||
/// <remarks>Your API credentials must allow event streaming.</remarks> | ||
/// <param name="apiKey">The API key.</param> | ||
/// <param name="apiSecret">The API secret.</param> | ||
public EventClient(string apiKey, string apiSecret) | ||
: this(null, apiKey, apiSecret){ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new event client and configures the provided event URL and api credentials. | ||
/// </summary> | ||
/// <remarks>Your API credentials must allow event streaming.</remarks> | ||
/// <param name="eventUrl">The event URL.</param> | ||
/// <param name="apiKey">The API key.</param> | ||
/// <param name="apiSecret">The API secret.</param> | ||
public EventClient(string eventUrl, string apiKey, string apiSecret) { | ||
_client = new ClientWebSocket(); | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.