Skip to content

Commit

Permalink
Enabled AzureAD integration and added TestAzureAD sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
gbablani committed Apr 17, 2013
1 parent 9e33a9e commit 789f14a
Show file tree
Hide file tree
Showing 159 changed files with 33,861 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ PrecompiledWeb
*.user
*.tmp
*.ldf
*.mdf
*.swx
.dotest
*.Publish.xml
Expand Down
77 changes: 77 additions & 0 deletions samples/DotNetOpenAuth.ApplicationBlock/AzureADClaims.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//-----------------------------------------------------------------------
// <copyright file="AzureADClaims.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.ApplicationBlock
{
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;

/// <summary>
/// Contains clains of a AzureAD token.
/// </summary>
/// <remarks>
/// Technically, this class doesn't need to be public, but because we want to make it serializable in medium trust, it has to be public.
/// </remarks>
[DataContract]
[EditorBrowsable(EditorBrowsableState.Never)]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "AzureAD", Justification = "Brand name")]
public class AzureADClaims
{
#region Public Properties

/// <summary>
/// Gets or sets the audience.
/// </summary>
/// <value> The audience token is valid for. </value>
[DataMember(Name = "aud")]
public string Aud { get; set; }

/// <summary>
/// Gets or sets the issuer.
/// </summary>
/// <value> The issuer. </value>
[DataMember(Name = "iss")]
public string Iss { get; set; }

/// <summary>
/// Gets or sets the early expiry time.
/// </summary>
/// <value> The early expiry time. </value>
[DataMember(Name = "nbf")]
public string Nbf { get; set; }

/// <summary>
/// Gets or sets the expiry time.
/// </summary>
/// <value> The expiry time. </value>
[DataMember(Name = "exp")]
public string Exp { get; set; }

/// <summary>
/// Gets or sets the id of the user.
/// </summary>
/// <value> The id of the user. </value>
[DataMember(Name = "oid")]
public string Oid { get; set; }

/// <summary>
/// Gets or sets the id of the tenant.
/// </summary>
/// <value> The tenant . </value>
[DataMember(Name = "tid")]
public string Tid { get; set; }

/// <summary>
/// Gets or sets the appid of application.
/// </summary>
/// <value> The id of the application. </value>
[DataMember(Name = "appid")]
public string Appid { get; set; }
#endregion
}
}
62 changes: 62 additions & 0 deletions samples/DotNetOpenAuth.ApplicationBlock/AzureADGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//-----------------------------------------------------------------------
// <copyright file="AzureADGraph.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.ApplicationBlock
{
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;

/// <summary>
/// Contains data of a AzureAD user.
/// </summary>
/// <remarks>
/// Technically, this class doesn't need to be public, but because we want to make it serializable in medium trust, it has to be public.
/// </remarks>
[DataContract]
[EditorBrowsable(EditorBrowsableState.Never)]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "AzureAD", Justification = "Brand name")]
public class AzureADGraph {
#region Public Properties

/// <summary>
/// Gets or sets the firstname.
/// </summary>
/// <value> The first name. </value>
[DataMember(Name = "givenName")]
public string GivenName { get; set; }

/// <summary>
/// Gets or sets the lastname.
/// </summary>
/// <value> The last name. </value>
[DataMember(Name = "surname")]
public string Surname { get; set; }

/// <summary>
/// Gets or sets the email.
/// </summary>
/// <value> The email. </value>
[DataMember(Name = "userPrincipalName")]
public string UserPrincipalName { get; set; }

/// <summary>
/// Gets or sets the fullname.
/// </summary>
/// <value> The fullname. </value>
[DataMember(Name = "displayName")]
public string DisplayName { get; set; }

/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value> The id. </value>
[DataMember(Name = "objectId")]
public string ObjectId { get; set; }
#endregion
}
}
51 changes: 51 additions & 0 deletions samples/DotNetOpenAuth.ApplicationBlock/AzureADHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//-----------------------------------------------------------------------
// <copyright file="AzureADHeader.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.ApplicationBlock
{
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;

/// <summary>
/// Contains header of AzureAD JWT token.
/// </summary>
/// <remarks>
/// Technically, this class doesn't need to be public, but because we want to make it serializable in medium trust, it has to be public.
/// </remarks>
[DataContract]
[EditorBrowsable(EditorBrowsableState.Never)]
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "AzureAD", Justification = "Brand name")]

public class AzureADHeader
{
#region Public Properties

/// <summary>
/// Gets or sets the type of token. Will always be JWT
/// </summary>
/// <value> The type of token. </value>
[DataMember(Name = "typ")]
public string Typ { get; set; }

/// <summary>
/// Gets or sets the algo of the header.
/// </summary>
/// <value> The algo of encoding. </value>
[DataMember(Name = "alg")]
public string Alg { get; set; }

/// <summary>
/// Gets or sets the thumbprint of the header.
/// </summary>
/// <value> The thumbprint of the cert used to encode. </value>
[DataMember(Name = "x5t")]
public string X5t { get; set; }

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel.Web" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
Expand All @@ -86,6 +87,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AzureADClaims.cs" />
<Compile Include="azureadclient.cs" />
<Compile Include="AzureADGraph.cs" />
<Compile Include="AzureADHeader.cs" />
<Compile Include="CustomExtensions\Acme.cs" />
<Compile Include="CustomExtensions\AcmeRequest.cs" />
<Compile Include="CustomExtensions\AcmeResponse.cs" />
Expand Down
112 changes: 112 additions & 0 deletions samples/DotNetOpenAuth.ApplicationBlock/azureadclient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//-----------------------------------------------------------------------
// <copyright file="AzureADClient.cs" company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.ApplicationBlock
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using DotNetOpenAuth.OAuth2;

public class AzureADClient : WebServerClient
{
private static readonly AuthorizationServerDescription AzureADDescription = new AuthorizationServerDescription
{
TokenEndpoint = new Uri("https://login.windows.net/global/oauth2/token"),
AuthorizationEndpoint = new Uri("https://login.windows.net/global/oauth2/authorize?resource=00000002-0000-0000-c000-000000000000/graph.windows.net"),
};

/// <summary>
/// Initializes a new instance of the <see cref="AzureADClient"/> class.
/// </summary>
public AzureADClient()
: base(AzureADDescription)
{
}

#region Methods

/// <summary>
/// Parses the access token into an AzureAD token.
/// </summary>
/// <param name="token">
/// The token as a string.
/// </param>
/// <returns>
/// The claims as an object and null in case of failure.
/// </returns>
public AzureADClaims ParseAccessToken(string token)
{
try
{
// This is the encoded JWT token split into the 3 parts
string[] strparts = token.Split('.');

// Decparts has the header and claims section decoded from JWT
string jwtHeader, jwtClaims;
string jwtb64Header, jwtb64Claims, jwtb64Sig;
byte[] jwtSig;
if (strparts.Length != 3)
{
return null;
}
jwtb64Header = strparts[0];
jwtb64Claims = strparts[1];
jwtb64Sig = strparts[2];
jwtHeader = Base64URLdecode(jwtb64Header);
jwtClaims = Base64URLdecode(jwtb64Claims);
jwtSig = Base64URLdecodebyte(jwtb64Sig);

JavaScriptSerializer s1 = new JavaScriptSerializer();

AzureADClaims claimsAD = s1.Deserialize<AzureADClaims>(jwtClaims);
AzureADHeader headerAD = s1.Deserialize<AzureADHeader>(jwtHeader);

return claimsAD;
}
catch (Exception)
{
return null;
}
}

/// <summary>
/// Base64 decode function except that it switches -_ to +/ before base64 decode
/// </summary>
/// <param name="str">
/// The string to be base64urldecoded.
/// </param>
/// <returns>
/// Decoded string as string using UTF8 encoding.
/// </returns>
private static string Base64URLdecode(string str)
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
return encoder.GetString(Base64URLdecodebyte(str));
}

/// <summary>
/// Base64 decode function except that it switches -_ to +/ before base64 decode
/// </summary>
/// <param name="str">
/// The string to be base64urldecoded.
/// </param>
/// <returns>
/// Decoded string as bytes.
/// </returns>
private static byte[] Base64URLdecodebyte(string str)
{
// First replace chars and then pad per spec
str = str.Replace('-', '+').Replace('_', '/');
str = str.PadRight(str.Length + ((4 - (str.Length % 4)) % 4), '=');
return Convert.FromBase64String(str);
}

#endregion
}
}
17 changes: 17 additions & 0 deletions samples/OAuthClient/AzureAD.aspx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AzureAD.aspx.cs" Inherits="OAuthClient.AzureAD" Async="true"%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Welcome,
<asp:Label Text="[name]" ID="nameLabel" runat="server" />
</div>
</form>
</body>
</html>
Loading

0 comments on commit 789f14a

Please sign in to comment.