Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bld/
# Visual Studo cache/options directory
.vs/
*/.vs/
**/Properties/*

# MSTest test Results
[Tt]est[Rr]esult*/
Expand Down
92 changes: 92 additions & 0 deletions NoarkWsClientSample/NoarkWsClientSample/DocumasterClients.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using CommandLine;
using Documaster.WebApi.Client.IDP;
using Documaster.WebApi.Client.IDP.Oauth2;
using Documaster.WebApi.Client.Noark5.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NoarkWsClientSample
{
public class DocumasterClients
{
protected NoarkClient noarkClient;
protected Oauth2HttpClient idpClient;

private string refreshToken;
private DateTime acccessTokenExpirationTime;
private Options opts;

public DocumasterClients(Options opts)
{
this.opts = opts;

/*
* Using the Noark 5 web services requires providing a valid access token.
* The way this token is obtained depends on the system implementing the services.
* This sample code obtains the token from the Documaster's identity provider service
* with the help of a designated Documaster IDP client.
* If the Noark client is used in the context of an application that has access to a web browser,
* we strongly recommend choosing the Oauth2 Authorization Code Grant Flow supported for obtaining
* access tokens.
*/

//Initialize an IDP client and request an authorization token
InitIdpClient(opts);

//Initialize a Noark client
InitClientWithoutClientCertificate(opts);

//Notice that it is also possible to initialize а ssl-based Noark client by providing a client certificate:
//InitClient(opts);
}

public NoarkClient getNoarkClient()
{
RefreshAccessToken();
return noarkClient;
}

private void RefreshAccessToken()
{
//access token expires in 60 minutes

if (refreshToken == null)
{
PasswordGrantTypeParams passwordGrantTypeParams = new PasswordGrantTypeParams(opts.ClientId, opts.ClientSecret, opts.Username, opts.Password, OpenIDConnectScope.OPENID);
var accessTokenResponse = idpClient.GetTokenWithPasswordGrantType(passwordGrantTypeParams);
acccessTokenExpirationTime = DateTime.Now.AddSeconds(accessTokenResponse.ExpiresInMs);
refreshToken = accessTokenResponse.RefreshToken;

noarkClient.AuthToken = accessTokenResponse.AccessToken;

}
else if (DateTime.Now > acccessTokenExpirationTime)
{
RefreshTokenGrantTypeParams refreshTokenGrantTypeParams = new RefreshTokenGrantTypeParams(refreshToken, opts.ClientId, opts.ClientSecret, OpenIDConnectScope.OPENID);
var accessTokenResponse = idpClient.RefreshToken(refreshTokenGrantTypeParams);
acccessTokenExpirationTime = DateTime.Now.AddSeconds(accessTokenResponse.ExpiresInMs);
refreshToken = accessTokenResponse.RefreshToken;

noarkClient.AuthToken = accessTokenResponse.AccessToken;
}
}

private void InitIdpClient(Options options)
{
idpClient = new Oauth2HttpClient(options.IdpServerAddress, true);
}

private void InitClient(Options options)
{
noarkClient = new NoarkClient(options.ServerAddress, true, options.CertificatePath, options.CertificatePass);
}

private void InitClientWithoutClientCertificate(Options options)
{
noarkClient = new NoarkClient(options.ServerAddress, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
<PackageReference Include="Newtonsoft.Json" Version="11.0.2">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="CommandLineParser" Version="2.2.1">
<PackageReference Include="CommandLineParser" Version="2.6.0">
<IncludeAssets>all</IncludeAssets>
</PackageReference>

<PackageReference Include="Documaster.WebApi.Client.Noark5" Version="0.10.0">
<PackageReference Include="Documaster.WebApi.Client.Noark5" Version="0.12.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
<PackageReference Include="Documaster.WebApi.Client.IDP" Version="1.0.0">
<PackageReference Include="Documaster.WebApi.Client.IDP" Version="1.1.0">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
</ItemGroup>
Expand Down
39 changes: 39 additions & 0 deletions NoarkWsClientSample/NoarkWsClientSample/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using CommandLine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NoarkWsClientSample
{
public class Options
{
[Option("idpaddr", Required = true, HelpText = "Idp server address")]
public string IdpServerAddress { get; set; }

[Option("clientid", Required = true, HelpText = "Idp Client Id")]
public string ClientId { get; set; }

[Option("clientsecret", Required = true, HelpText = "Idp Client Secret")]
public string ClientSecret { get; set; }

[Option("username", Required = true, HelpText = "Username")]
public string Username { get; set; }

[Option("password", Required = true, HelpText = "Password")]
public string Password { get; set; }

[Option("addr", Required = true, HelpText = "Server address")]
public string ServerAddress { get; set; }

[Option("cert", HelpText = "Path to certificate file")]
public string CertificatePath { get; set; }

[Option("certpass", HelpText = "Certificate password")]
public string CertificatePass { get; set; }

[Option("testdoc", Required = true, HelpText = "Path to test file")]
public string TestDoc { get; set; }
}
}
Loading