Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzorz committed May 11, 2023
1 parent 0324e93 commit e890805
Show file tree
Hide file tree
Showing 20 changed files with 909 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Windows ignores
Thumbs.db
Thumbs.db.meta

# Mac ignores
*.DS_Store
*.directory

# Add your custom repo-wide rules below:
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# contenda-dotnet-sdk
Contenda .NET SDK
# Contenda .NET SDK

**Contenda** is the content catalyst for developer advocates.

A .NET Standard 2.0 library to help you use Contenda's APIs in your .NET projects!

![](https://img.shields.io/badge/platform-any-green.svg?longCache=true&style=flat-square) ![](https://img.shields.io/badge/nuget-yes-green.svg?longCache=true&style=flat-square) ![](https://img.shields.io/badge/license-MIT-blue.svg?longCache=true&style=flat-square)

## Usage

Refer to the `Contenda.SdkDemo` project to see example code using this SDK.

Make sure to call `Authenticate` before calling any APIs that require authentication - presently this includes all of them, except for the service health endpoint.

## Need more help?

💡 Read more about Contenda's API here https://prod.contenda.io/docs.

💬 Find us on twitter [@ContendaCo](https://twitter.com/ContendaCo) or join our public [Discord](https://discord.gg/bYda4pQz2v).
219 changes: 219 additions & 0 deletions sources/Contenda/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
# Created by https://www.gitignore.io/api/visualstudio

### VisualStudio ###
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
# x64/
# x86/
build/
bld/
[Bb]in/
[Oo]bj/

# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

# NUNIT
*.VisualState.xml
TestResult.xml

# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c

# DNX
project.lock.json
artifacts/

*_i.c
*_p.c
*_i.h
*.ilk
*.obj
*.meta
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc

# Chutzpah Test files
_Chutzpah*

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# TFS 2012 Local Workspace
$tf/

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user

# JustCode is a .NET coding add-in
.JustCode

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*

# MightyMoose
*.mm.*
AutoTest.Net/

# Web workbench (sass)
.sass-cache/

# Installshield output folder
[Ee]xpress/

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish/

# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj

# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config

# Windows Azure Build Output
csx/
*.build.csdef

# Windows Store app package directory
AppPackages/

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/

# Others
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.dbproj.schemaview
# *.pfx
*.publishsettings
node_modules/
orleans.codegen.cs

# RIA/Silverlight projects
Generated_Code/

# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm

# SQL Server files
*.mdf
*.ldf

# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings

# Microsoft Fakes
FakesAssemblies/

# Node.js Tools for Visual Studio
.ntvs_analysis.dat

# Visual Studio 6 build log
*.plg

# Visual Studio 6 workspace options file
*.opt

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
103 changes: 103 additions & 0 deletions sources/Contenda/Contenda.Sdk/Auth/APIKeyAuthProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Contenda.Sdk.Exceptions;
using Contenda.Sdk.Models.Request;
using Contenda.Sdk.Models.Result;
using Newtonsoft.Json;

namespace Contenda.Sdk.Auth
{
/// <summary>
/// API Key Authentication Provider
/// </summary>
public sealed class APIKeyAuthProvider : IAuthProvider
{
private readonly string _email;
private readonly string _apiKey;

private readonly DateTime _invalidDate = new(1970, 1, 1);

private string? _apiToken;

/// <summary>
/// Constructor for an API key auth provider
/// </summary>
/// <param name="email">user's email</param>
/// <param name="apiKey">api key</param>
public APIKeyAuthProvider(string email, string apiKey)
{
_email = email;
_apiKey = apiKey;
}

private DateTime ValidUntil()
{
if (_apiToken == null) return _invalidDate;
try
{
var handler = new JwtSecurityTokenHandler();
var token = handler.ReadJwtToken(_apiToken);
return token.ValidTo;
}
catch
{
return _invalidDate;
}
}

/// <inheritdoc />
public async Task<bool> TryAuthenticate(string apiBaseUri)
{
if (EnsureValid()) return true;

var client = PoorMansHttpClientFactory.Instance.Client;

var uri = $"{apiBaseUri}{Constants.Version2Prefix}{Constants.IdentityV2.Token}";
var body = new TokenV2
{
api_key = _apiKey,
email = _email
};
var bodyString = JsonConvert.SerializeObject(body);

try
{
var response = await client.PostAsync(new Uri(uri), new StringContent(bodyString, Encoding.UTF8, Constants.JsonMimeType)).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var tokenResponse = await response.Content.ReadAsStringAsync();
var tokenResult = JsonConvert.DeserializeObject<TokenResult>(tokenResponse);
_apiToken = tokenResult!.access_token;
return true;
}
catch
{
return false;
}
}

private bool EnsureValid() => DateTime.Now < ValidUntil();

/// <inheritdoc />
public void ModifyHeadersCallback(HttpClient httpClient)
{
if (!EnsureValid()) throw new AuthenticationException("You have to be authenticated to call APIs with authentication.");
// nothing to do here
}

/// <inheritdoc />
public string ModifyQueryCallback(string currentQuery)
{
if (!EnsureValid()) throw new AuthenticationException("You have to be authenticated to call APIs with authentication.");

var nvc = HttpUtility.ParseQueryString((new Uri(currentQuery)).Query);
var paramChar = nvc.Count == 0 ? "?" : "&";
return $"{currentQuery}{paramChar}token={HttpUtility.UrlEncode(_apiToken)}";
}


}
}
31 changes: 31 additions & 0 deletions sources/Contenda/Contenda.Sdk/Auth/IAuthProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Net.Http;
using System.Threading.Tasks;

namespace Contenda.Sdk.Auth
{
/// <summary>
/// Authentication provider interface
/// </summary>
public interface IAuthProvider
{
/// <summary>
/// Modify headers callback
/// </summary>
/// <param name="httpClient">the HttpClient with the headers to modify</param>
void ModifyHeadersCallback(HttpClient httpClient);

/// <summary>
/// Modify query callback
/// </summary>
/// <param name="currentQuery">the query to modify</param>
/// <returns>modified query</returns>
string ModifyQueryCallback(string currentQuery);

/// <summary>
/// Try authenticate
/// </summary>
/// <param name="apiBaseUri">API Base URI</param>
/// <returns>true if successfully authenticated, false otherwise</returns>
Task<bool> TryAuthenticate(string apiBaseUri);
}
}
Loading

0 comments on commit e890805

Please sign in to comment.