Skip to content

Commit

Permalink
Merge pull request #133 from eaba/oauth2
Browse files Browse the repository at this point in the history
Oauth2
  • Loading branch information
eaba committed Jul 17, 2022
2 parents 9627662 + 84f7fa6 commit 2854170
Show file tree
Hide file tree
Showing 27 changed files with 1,372 additions and 209 deletions.
112 changes: 0 additions & 112 deletions SharpPulsar/Auth/AuthenticationDataOAuth2.cs

This file was deleted.

4 changes: 0 additions & 4 deletions SharpPulsar/Auth/AuthenticationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public static IAuthentication Token(string token)
return DefaultImplementation.NewAuthenticationToken(token);
}

public static IAuthentication Sts(string client, string secret, string authority)
{
return DefaultImplementation.NewAuthenticationSts(client, secret, authority);
}
/// <summary>
/// Create an authentication provider for token based authentication.
/// </summary>
Expand Down
88 changes: 0 additions & 88 deletions SharpPulsar/Auth/AuthenticationOAuth2.cs

This file was deleted.

70 changes: 70 additions & 0 deletions SharpPulsar/Auth/OAuth2/AuthenticationDataOAuth2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using SharpPulsar.Interfaces;

/// <summary>
/// Licensed to the Apache Software Foundation (ASF) under one
/// or more contributor license agreements. See the NOTICE file
/// distributed with this work for additional information
/// regarding copyright ownership. The ASF licenses this file
/// to you under the Apache License, Version 2.0 (the
/// "License"); you may not use this file except in compliance
/// with the License. You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing,
/// software distributed under the License is distributed on an
/// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
/// KIND, either express or implied. See the License for the
/// specific language governing permissions and limitations
/// under the License.
/// </summary>
namespace SharpPulsar.Auth.OAuth2
{
/// <summary>
/// Provide OAuth 2.0 authentication data.
/// </summary>
[Serializable]
internal class AuthenticationDataOAuth2 : IAuthenticationDataProvider
{
public const string HttpHeaderName = "Authorization";

private readonly string _accessToken;
private readonly ISet<KeyValuePair<string, string>> _headers = new HashSet<KeyValuePair<string, string>>() ;

public AuthenticationDataOAuth2(string accessToken)
{
_accessToken = accessToken;
_headers.Add(new KeyValuePair<string, string>(HttpHeaderName, "Bearer " + accessToken));
}

public virtual bool HasDataForHttp()
{
return true;
}

public virtual ISet<KeyValuePair<string, string>> HttpHeaders
{
get
{
return _headers;
}
}

public virtual bool HasDataFromCommand()
{
return true;
}

public virtual string CommandData
{
get
{
return _accessToken;
}
}

}

}
61 changes: 61 additions & 0 deletions SharpPulsar/Auth/OAuth2/AuthenticationFactoryOAuth2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using SharpPulsar.Interfaces;
/// <summary>
/// Licensed to the Apache Software Foundation (ASF) under one
/// or more contributor license agreements. See the NOTICE file
/// distributed with this work for additional information
/// regarding copyright ownership. The ASF licenses this file
/// to you under the Apache License, Version 2.0 (the
/// "License"); you may not use this file except in compliance
/// with the License. You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing,
/// software distributed under the License is distributed on an
/// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
/// KIND, either express or implied. See the License for the
/// specific language governing permissions and limitations
/// under the License.
/// </summary>
namespace SharpPulsar.Auth.OAuth2
{
/// <summary>
/// Factory class that allows to create <seealso cref="Authentication"/> instances
/// for OAuth 2.0 authentication methods.
/// </summary>
public sealed class AuthenticationFactoryOAuth2
{

/// <summary>
/// Authenticate with client credentials.
/// </summary>
/// <param name="issuerUrl"> the issuer URL </param>
/// <param name="credentialsUrl"> the credentials URL </param>
/// <param name="audience"> An optional field. The audience identifier used by some Identity Providers, like Auth0. </param>
/// <returns> an Authentication object </returns>
public static IAuthentication ClientCredentials(Uri issuerUrl, Uri credentialsUrl, string audience)
{
return ClientCredentials(issuerUrl, credentialsUrl, audience, null);
}

/// <summary>
/// Authenticate with client credentials.
/// </summary>
/// <param name="issuerUrl"> the issuer URL </param>
/// <param name="credentialsUrl"> the credentials URL </param>
/// <param name="audience"> An optional field. The audience identifier used by some Identity Providers, like Auth0. </param>
/// <param name="scope"> An optional field. The value of the scope parameter is expressed as a list of space-delimited,
/// case-sensitive strings. The strings are defined by the authorization server.
/// If the value contains multiple space-delimited strings, their order does not matter,
/// and each string adds an additional access range to the requested scope.
/// From here: https://datatracker.ietf.org/doc/html/rfc6749#section-4.4.2 </param>
/// <returns> an Authentication object </returns>
public static IAuthentication ClientCredentials(Uri issuerUrl, Uri credentialsUrl, string audience, string scope)
{
var flow = new ClientCredentialsFlow(issuerUrl, audience, credentialsUrl.LocalPath, scope);
return new AuthenticationOAuth2(flow, DateTime.Now);
}
}

}
Loading

0 comments on commit 2854170

Please sign in to comment.