/// <summary> /// Attempts to connect to SharePoint and creates a <see cref="CredentialedSharePointConnectionInfo"/> returned as application/json /// </summary> /// <param name="args">An <see cref="CreateCredentialTokenFunctionArgs"/> instance specifying the location of the client configuration in Azure storage.</param> public HttpResponseMessage Execute(CreateCredentialTokenFunctionArgs args) { //If the input is bad in any way there will be an error and the response shall be Unauthorized try { if (_credentialedSharePointConnectionInfo == null) { throw new InvalidOperationException("The input didn't have connection info in json format"); } var clientConfig = GetConfiguration(_credentialedSharePointConnectionInfo.ClientId); //This will throw if the connection info is no good _credentialedSharePointConnectionInfo.GetSharePointClientContext(); var token = _credentialedSharePointConnectionInfo.GetEncryptedToken(clientConfig.CredentialedClientConfig.Password, clientConfig.CredentialedClientConfig.Salt); _response.StatusCode = HttpStatusCode.OK; _response.Content = new StringContent($"{{\"credentialToken\":\"{token}\"}}"); _response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(_response); } catch (Exception ex) { Log($"Error connecting {ex}"); _response.StatusCode = HttpStatusCode.Unauthorized; return(_response); } }
/// <summary> /// Validates the current request /// </summary> /// <param name="args">An <see cref="ValidateCredentialTokenFunctionArgs"/> instance specifying the location of the client configuration in Azure storage.</param> /// <returns>OK with json '{"valid": true || false}'</returns> public HttpResponseMessage Execute(ValidateCredentialTokenFunctionArgs args) { var responseObject = new { valid = false }; //If the input is bad in any way there will be an error and the response shall be OK with json {"valid":false} try { if (!_queryParams.ContainsKey("token") || !_queryParams.ContainsKey("cId")) { throw new Exception(); } var clientConfig = GetConfiguration(_queryParams["cId"]); //Will throw if the token can't be decrypted var ctx = CredentialedSharePointConnectionInfo.GetSharePointClientContext(_queryParams["token"], clientConfig.CredentialedClientConfig.Password, clientConfig.CredentialedClientConfig.Salt); ctx.Load(ctx.Web); //Will throw if the credentials are no good ctx.ExecuteQuery(); responseObject = new { valid = true }; } catch { //ignored - if there is an exception the request is simply not valid } _response.StatusCode = HttpStatusCode.OK; _response.Content = new StringContent((new JavaScriptSerializer()).Serialize(responseObject)); _response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(_response); }