Skip to content

fokuspokus/proton-cs-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Proton API C# Client

Licence Apache 2.0

Description

This library is a C# (netstandard 2.0) implementation of Proton API Client. It is meant for use by C# applications that want to authenticate at Proton server.

The official Python client can be found here.

Dependencies

Installation

For installation clone this repository recursively with submodules:

git clone --recurse-submodules -j8 git://github.com/Eppie-io/proton-cs-client

Usage

Setup

Create new Proton session. For test run use these parameters:

using Tuvi.Proton.Client;

var proton = new Session(
	httpClient: new HttpClient(),
	host: new Uri("https://example.api.com"))
{
	AppVersion = "GithubExample_0.0.1", 
	UserAgent = "Ubuntu_20.04", 
	ClientSecret = string.Empty, 
	RedirectUri = new Uri("https://redirect.api.com")
};

Authentication

Authenticate by calling LoginAsync. Provide username and password.

Next, check if two-factor authentication is enabled. If so, provide 2FA code and TOTP with ProvideTwoFactorCodeAsync.

await proton.LoginAsync(
	username: "user@proton.me",
	password: "password",
	cancellationToken: CancellationToken.None);

if (proton.IsTwoFactor && proton.IsTOTP)
{
	await proton.ProvideTwoFactorCodeAsync(
		code: "123456", 
		cancellationToken: CancellationToken.None);
}

Logout

LogoutAsync closes the session.

await proton.LogoutAsync(cancellationToken: CancellationToken.None);

Refresh Session

RefreshAsync asynchronously refreshes AccessToken.

await proton.RefreshAsync(cancellationToken: CancellationToken.None);

Store session

If you want to store the session for, call Dump. The return value will contain JSON data, that can be passed later to Load and RefreshAsync.

string protonDump = proton.Dump();
Save(protonDump);

Sample result:

{
	"Version":1, 
	"Uid":"7ry2z7aydqhqir4a3xe7pcyqyqblkzmp",
	"AccessToken":"xvth2getrrhfuvvw5lfnkd7k3esfdbz7",
	"TokenType":"Bearer",
	"RefreshToken":"n4am4teh7htzbkhjyr2rg4fsut35ec46",
	"PasswordMode":1,
	"Scope":"full self payments keys parent user loggedin nondelinquent mail vpn calendar drive pass verified"
}

Load session

To Load previvously saved session, provide a JSON formatted string created by Dump:

string protonDump = ExampleLoadProtonDumpMethod();
proton.Load(dump: protonDump);

API Calls

After successful authentication you are ready to make API calls to Proton server. For more information on available requests refer to official Proton API documentation.

RequestAsync makes a request asynchronously.

The following demo shows how to count the number of messages in the maillbox. This will return a value of type TotalMessagesContent that results from deserializing the content as JSON.

Example:

// TotalMessagesContent type
struct TotalMessagesContent
{
	public int Code { get; set; } 
	public string Error { get; set; } 
	public JsonObject Details { get; set; } 
	
	public IList<Folder> Counts { get; set; } 

	public record Folder
	{
		public string LabelID { get; set; } 
		public long Total { get; set; } 
		public long Unread { get; set; }
	}
}

// api request
TotalMessagesContent countResponse = await proton.RequestAsync<TotalMessagesContent>(
	endpoint: new Uri("/mail/v4/messages/count", UriKind.Relative),
	method: HttpMethod.Get,
	headers: null,
	cancellationToken: CancellationToken.None);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%