예제 #1
0
        string GetAuthenticationRequestBody(string userName, string password, UcwaAppAuthenticationTypes authType)
        {
            string requestBody = null;

            switch (authType)
            {
            case UcwaAppAuthenticationTypes.Windows:
                requestBody = "grant_type=urn:microsoft.rtc:windows&username="******"grant_type=urn:microsoft.rtc:anonmeeting&password="******"&msrtc_conferenceuri=" + userName;
                break;

            case UcwaAppAuthenticationTypes.Passive:
                requestBody = "grant_type=urn:Microsoft.rtc:passive";
                break;

            default:      // password
                requestBody = "grant_type=password&username="******"&password=" + password;
                break;
            }

            return(requestBody);
        }
예제 #2
0
        public void SetAuthenticationType(UcwaAppAuthenticationTypes authType)
        {
            var accept        = this.Accept;
            var authorization = this.Authorization;
            var baseAddress   = this.BaseAddress;

            Client.Dispose();
            Client = null;
            Initialize(accept, authorization, baseAddress, authType);
            this.AuthenticationType = authType;
        }
예제 #3
0
 public void Initialize(string accept, AuthenticationHeaderValue authorization,
                        Uri baseAddress, UcwaAppAuthenticationTypes authType = UcwaAppAuthenticationTypes.Password)
 {
     Client = new HttpClient(new HttpClientHandler {
         UseDefaultCredentials = authType == UcwaAppAuthenticationTypes.Windows
     });
     Client.Timeout          = new TimeSpan(0, 30, 0); // initialize the app timeout to be 30 minutes.
     this.AuthenticationType = authType;
     this.SetAccept(accept);
     if (baseAddress != null)
     {
         this.Client.BaseAddress = baseAddress;                      // must be set before any request is made
     }
     if (authorization != null)
     {
         this.SetAuthorization(authorization);
         this.Client.DefaultRequestHeaders.Authorization = authorization;
     }
 }
예제 #4
0
        public async Task <HttpStatusCode> SignIn(string userName, string password, UcwaAppAuthenticationTypes authType = UcwaAppAuthenticationTypes.Password)
        {
            this.userName           = userName;
            this.password           = password;
            this.authenticationType = authType;
            try
            {
                var opResult = await DiscoverRootResource(this.discoverFromInternalDomain);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "GetRootResource returns null result.", opResult.StatusCode);
                    return(opResult.StatusCode);
                }

                opResult = await GetUserResource(opResult.Resource.GetLinkUri("user"), userName, password, authType);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported,
                                                userName + " cannot be authenticated, with the " + authType.ToString() + " grant_type.",
                                                opResult.StatusCode);
                    return(opResult.StatusCode);
                }
                // Create the UCWA application bound to the specified user
                opResult = await GetApplicationResource(opResult.Resource);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to create the UCWA application resource.", opResult.StatusCode);
                    return(opResult.StatusCode);
                }

                this.ApplicationResource = opResult.Resource;
                UcwaAppUtils.ReportProgress(OnProgressReported, "Succeded in creating the application resource: " + this.ApplicationResource.Uri);

                // Setup and start event channel
                var eventsUri = this.ApplicationResource.GetLinkUri("events");
                this.EventChannel = new UcwaAppEventChannel(eventsUri, this.Transport);
                this.EventChannel.OnEventNotificationsReceived += EventChannel_OnEventNotificationsReceived;
                this.EventChannel.OnErrorReported      += EventChannel_OnErrorReported;
                this.EventChannel.OnEventChannelClosed += EventChannel_OnEventChannelClosed;
                this.EventChannel.OnProgressReported   += EventChannel_OnProgressReported;
                this.EventChannel.Start();
                UcwaAppUtils.ReportProgress(OnProgressReported, "Event channel started on " + eventsUri);

                // Make me available to receiving incoming alerts
                this.Me = new UcwaAppMe(this);
                var result = await this.Me.PostMakeMeAvailable("4255552222", "Online", new string[] { "Plain", "Html" }, new string[] { "PhoneAudio", "Messaging" });

                if (result.StatusCode != HttpStatusCode.NoContent)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to post to makeMeAvailable resource.", result.StatusCode);
                    return(result.StatusCode);
                }

                // Get application resource again to receive any updates triggered by the POST request to making me available
                opResult = await GetApplicationResource(this.ApplicationResource.Uri);

                if (opResult.Resource == null)
                {
                    UcwaAppUtils.ReportProgress(OnProgressReported, "Failed to get the updated application resource", opResult.StatusCode);
                    return(opResult.StatusCode);
                }
                this.ApplicationResource = opResult.Resource;
                await this.Me.Refresh(this.ApplicationResource.GetEmbeddedResourceUri("me"));

                // Set up a timer to post on reportMyActivity every four minutes
                timer = new Timer((e) =>
                                  { ReportMyActivity(this.ApplicationResource.GetEmbeddedResource("me").GetLinkUri("reportMyActivity")); },
                                  null,
                                  new TimeSpan(0, 0, 0),
                                  new TimeSpan(0, 4, 0)
                                  );
            }
            catch (Exception ex)
            {
                UcwaAppUtils.ReportError(OnErrorReported, ex);
                return(HttpStatusCode.BadRequest);
            }
            return(HttpStatusCode.OK);
        }
예제 #5
0
        IEnumerable <KeyValuePair <string, string> > CreateAuthRequestPayload(string userName, string password, UcwaAppAuthenticationTypes authType)
        {
            KeyValuePair <string, string>[] formData = new KeyValuePair <string, string>[] {};
            var formDataList = formData.ToList();

            switch (authType)
            {
            case UcwaAppAuthenticationTypes.Windows:
                formDataList.Add(new KeyValuePair <string, string>("grant_type", "urn:microsoft.rtc:windows"));
                formDataList.Add(new KeyValuePair <string, string>("username", userName));
                break;

            case UcwaAppAuthenticationTypes.Annonymous:
                formDataList.Add(new KeyValuePair <string, string>("grant_type", "urn:microsoft.rtc:anonmeeting"));
                formDataList.Add(new KeyValuePair <string, string>("password", password));
                formDataList.Add(new KeyValuePair <string, string>("msrtc_conferenceuri", userName));
                break;

            case UcwaAppAuthenticationTypes.Passive:
                formDataList.Add(new KeyValuePair <string, string>("grant_type", "urn:microsoft.rtc:passive"));
                break;

            default:      // password
                formDataList.Add(new KeyValuePair <string, string>("grant_type", "password"));
                formDataList.Add(new KeyValuePair <string, string>("password", password));
                formDataList.Add(new KeyValuePair <string, string>("username", userName));
                break;
            }
            ;

            return(formDataList.AsEnumerable());
        }
예제 #6
0
        private async Task <UcwaAppOperationResult> GetUserResource(string userResUri, string userName, string password, UcwaAppAuthenticationTypes authType = UcwaAppAuthenticationTypes.Password)
        {
            this.IsSignedIn = false;
            //
            // First GET user resource to retrieve oAuthToken href.
            // Expect 401 Unauthorized response as an HTML payload
            var result = await Transport.GetResourceAsync(userResUri);

            if (result.StatusCode != HttpStatusCode.Unauthorized && result.StatusCode != HttpStatusCode.OK)
            {
                return(new UcwaAppOperationResult(result.StatusCode, new Exception("Failed to GetRequest on " + userResUri)));
            }
            if (result.StatusCode == HttpStatusCode.Unauthorized)
            {
                // Get OAuth resource for a Web ticket
                if (result.ResponseHeaders.Contains("WWW-Authenticate"))
                {
                    var authServiceUri  = ParseOAuthServiceUri(result.ResponseHeaders);
                    var requestFormData = CreateAuthRequestPayload(userName, password, authType);

                    // Note: the following PostRequest returns a json payload in the responseData, containing the access token,
                    // as charset ('utf-8')?
                    result = await Transport.PostResourceAsync(authServiceUri, requestFormData);

                    var oAuth20Token = GetOAuthToken(result.ResponseBody);
                    SetTotRefreshOAuthTokenOnExpiration(oAuth20Token.ExpirationTime);
                    if (oAuth20Token != null)
                    {
                        Transport.SetAuthorization(new AuthenticationHeaderValue(oAuth20Token.GrantType, oAuth20Token.AccessToken));
                        // Second GET userHref, supplying the required compact-web-ticket (cwt) in an Authorization header
                        result = await Transport.GetResourceAsync(userResUri);

                        if (result.StatusCode != HttpStatusCode.OK)
                        {
                            return(new UcwaAppOperationResult(
                                       result.StatusCode, result.ResponseHeaders, result.ResponseBody,
                                       new Exception("GetRequest on " + userResUri + " with oAuth token of " + oAuth20Token)));
                        }
                    }
                    else
                    {
                        return(new UcwaAppOperationResult(result.StatusCode, result.ResponseHeaders, result.ResponseBody, new Exception("Invalid access token")));
                    }
                }
            }
            this.IsSignedIn = true;
            return(result);
        }
 public SignInParameter(string name, string pass, UcwaAppAuthenticationTypes authType)
 {
     this.UserName = name;
     this.Password = pass;
     this.AuthType = authType;
 }