Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add oauth support and an oauth prompt #520

Merged
merged 6 commits into from
May 22, 2018
Merged

add oauth support and an oauth prompt #520

merged 6 commits into from
May 22, 2018

Conversation

swagatmishra2007
Copy link
Member

the following changes are part of this PR -

  1. added support for an oauth client and bubbled up the methods via the botframework adapter.
  2. added an oauth prompt

Copy link
Member

@johnataylor johnataylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will need changes following the updates to dialogs, so lets do this after we have those changes in

@@ -0,0 +1,95 @@
using System;
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs copyright(s) at the top. #Resolved


namespace Microsoft.Bot.Builder.Dialogs
{
public class OAuthPromptSettingsWithTimeout : prompts.OAuthPromptSettings
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XML Code Comments are needed on all public classes and all public methods. #Resolved

@@ -0,0 +1,95 @@
using System;
using prompts = Microsoft.Bot.Builder.Prompts;
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the import like this? Just do a standard using and avoid the "prompts." everywhere. #Resolved

using static Microsoft.Bot.Builder.Prompts.PromptValidatorEx;
using Microsoft.Bot.Schema;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Prompts;
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort the usings. Be sure the "System" usings are at the top, and custom usings are last. There's a VS Setting for this. #Resolved

public int? Timeout { get; set; }
}

public class OAuthPromptState : PromptOptions
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XML Comment.s #Resolved

throw new ValidationException(ValidationRules.CannotBeNull, "userId");
}

bool _shouldTrace = ServiceClientTracing.IsEnabled;
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comments around "_" variables. #Resolved

{
if (connectionName == null)
{
throw new ValidationException(ValidationRules.CannotBeNull, "connectionName");
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this just "ArgumentNullException()" ? #Resolved

return String.Empty;
}

public async Task SendEmulateOAuthCardsAsync(bool emulateOAuthCards)
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of the same comments. #Resolved

/// OAuthCard ContentType value
public partial class OAuthCard
{
public const string ContentType = "application/vnd.microsoft.card.oauth";
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful code comments. :) #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is good enough though right? it's pretty much just defining a constant (contenttype) for this class).


In reply to: 184823915 [](ancestors = 184823915)

@@ -0,0 +1,34 @@
using System;
Copy link
Contributor

@cleemullins cleemullins Apr 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//Copyright #Resolved

Copy link
Contributor

@cleemullins cleemullins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is looking great.

Tests are really the next big thing. We can't check this in w/o Unit tests...


var cards = activity.Attachments.Where(a => a.Content is OAuthCard);
if (cards.Count() == 0)
throw new InvalidOperationException("OAuthPrompt.Prompt(): atleast one of the cards should be an oauth card");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"atleast" => "at least"

/// <returns></returns>
public async Task Prompt(ITurnContext context, string text, string speak = null)
{
if (context == null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a BotAssert. Method that will do this for you. Applies through for context==null checks.

{
if (context == null)
throw new ArgumentNullException(nameof(context));
if (string.IsNullOrEmpty(text))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably mean IsNullOrWhitespace()


await context.SendActivity(text, speak).ConfigureAwait(false);

var adapter = context.Adapter as BotFrameworkAdapter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and the similar check above) is going to make Testing really hard.

Because we test via the "TestAdapter" I suspect you'll want to factor the necessary bits into an interface "IOAuthAdapter" and allow both the Bot Framework adapter and the Test adapter to implement.

{
Uri uriResult;
if (!(Uri.TryCreate(uri, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttps))
throw new InvalidOperationException("Please supply a valid https uri");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArgumentException

ServiceClientTracing.Enter(invocationId, this, "GetUserTokenAsync", tracingParameters);
}
// Construct URL
var tokenUrl = new Uri(new Uri(uri + (uri.EndsWith("/") ? "" : "/")), "api/usertoken/GetToken?userId={userId}&connectionName={connectionName}{magicCodeParam}").ToString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move all scary magic strings into constants with detailed comments on them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i want to keep this logic as such for now, since it's in sync wtih v3 sdk and node as well (v3 and v4) . i will update all sdk's to use a proper uribuilder for this logic in another review.

return tokenResponse;
}
catch (JsonException)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception handling seems... incorrect. If it's right, please add comments in here.

}
else if (statusCode == HttpStatusCode.NotFound)
{
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can imagine tracing here would be very handy for debugging.

/// <returns></returns>
public async Task<bool> SignOutUserAsync(string userId, string connectionName, CancellationToken cancellationToken = default(CancellationToken))
{
if (string.IsNullOrEmpty(userId))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got these all throughout the code. Consider adding:
private static void AssertValidUserId(string userId)
{
if (string.isNullOrWhitespace(userId)) throw new ArgumentNullExcpetion(nameof(userId))
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's in just a couple of places and more descriptive, so i dont think it's worth encapsulating in anohter method.


namespace Microsoft.Bot.Schema
{
public class TokenExchangeState
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

///

@cleemullins
Copy link
Contributor

@swagatmishra2007

Please make sure to rebase against master, as there's been drift in Prompts / Dialog classes since you started.

@cleemullins cleemullins merged commit 93eb531 into microsoft:master May 22, 2018
tomlm pushed a commit that referenced this pull request Nov 27, 2018
add oauth support and an oauth prompt
ShYuPe pushed a commit to ShYuPe/botbuilder-dotnet that referenced this pull request Aug 25, 2020
* exporting teams from botbuilder schema

* black:exporting teams from botbuilder schema
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants