yavsc/testOauthClient/Controllers/HomeController.cs

160 lines
5.9 KiB
C#
Raw Normal View History

2016-06-01 23:47:06 +02:00
using System;
2018-03-12 23:49:05 +01:00
using System.Collections.Generic;
using System.IO;
2016-07-27 10:44:41 +02:00
using System.Json;
2018-03-12 23:49:05 +01:00
using System.Linq;
using System.Net;
2016-06-13 13:33:32 +02:00
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
2016-06-01 23:47:06 +02:00
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
2016-07-27 10:44:41 +02:00
using Microsoft.Extensions.Logging;
2018-03-12 23:49:05 +01:00
using Yavsc.Server.Helpers;
using Yavsc.Server.Model;
2016-06-01 23:47:06 +02:00
namespace testOauthClient.Controllers
{
public class HomeController : Controller
{
2016-07-27 10:44:41 +02:00
ILogger _logger;
2018-03-12 23:49:05 +01:00
2016-07-27 10:44:41 +02:00
public HomeController(ILoggerFactory loggerFactory)
{
_logger=loggerFactory.CreateLogger<HomeController>();
}
2016-06-13 13:33:32 +02:00
[HttpGet]
2016-06-01 23:47:06 +02:00
public IActionResult Index()
{
return View();
}
2016-06-13 13:33:32 +02:00
[HttpPost]
public async Task<IActionResult> GetUserInfo(CancellationToken cancellationToken)
{
2016-07-27 10:44:41 +02:00
using (var client = new HttpClient())
{
2016-06-13 13:33:32 +02:00
var request = new HttpRequestMessage(HttpMethod.Get, "http://dev.pschneider.fr/api/me");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
2016-07-27 10:44:41 +02:00
2016-06-13 13:33:32 +02:00
var response = await client.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
return View("Index", model: await response.Content.ReadAsStringAsync());
}
2016-06-01 23:47:06 +02:00
2016-06-13 13:33:32 +02:00
}
2016-07-27 10:44:41 +02:00
2018-03-12 23:49:05 +01:00
[HttpPost]
public async Task<IActionResult> PostFiles(string subdir)
{
string results = null;
_logger.LogInformation($"{Request.Form.Files.Count} file(s) to send");
// TODO better uri construction in production environment
List<FormFile> args = new List<FormFile>();
foreach (var formFile in Request.Form.Files)
{
_logger.LogWarning($"Treating {formFile.ContentDisposition}");
var memStream = new MemoryStream();
const int sz = 1024*64;
byte [] buffer = new byte[sz];
using (var innerStream = formFile.OpenReadStream()) {
int szRead = 0;
do {
szRead = innerStream.Read(buffer,0,sz);
memStream.Write(buffer,0,szRead);
} while (szRead>0);
}
memStream.Seek(0,SeekOrigin.Begin);
args.Add(
new FormFile {
ContentDisposition = formFile.ContentDisposition,
ContentType = formFile.ContentType,
Stream = memStream
});
}
string uri = "http://dev.pschneider.fr/api/fs/"+System.Uri.EscapeDataString(subdir);
_logger.LogInformation($"Posting data to '{uri}'...");
results = await RequestHelper.PostMultipart(uri, args.ToArray(), AccessToken);
_logger.LogInformation("Data posted.");
return View("Index", model: results);
}
2016-07-19 15:57:46 +02:00
[HttpPost]
public async Task<IActionResult> PostDeviceInfo(CancellationToken cancellationToken)
{
2016-07-27 10:44:41 +02:00
/*
using (var client = new HttpClient()) {
var request = new HttpRequestMessage(HttpMethod.Post, "http://dev.pschneider.fr/api/gcm/register");
2016-07-19 15:57:46 +02:00
2016-07-27 10:44:41 +02:00
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
var json = JsonConvert.
SerializeObject(new Yavsc.Models.Identity.GoogleCloudMobileDeclaration { DeviceId= "devid01", GCMRegistrationId = "1234" } );
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.SendAsync(request, cancellationToken);
response.EnsureSuccessStatusCode();
return View("Index", model: await response.Content.ReadAsStringAsync());
}*/
JsonValue result = null;
var authHeader = $"Bearer {AccessToken}";
_logger.LogWarning($"using authorization Header {authHeader}");
try {
using (var request = new SimpleJsonPostMethod(
"http://dev.pschneider.fr/api/gcm/register", authHeader))
{
result = await request.InvokeJson(new
{
GCMRegistrationId = "testGoogleRegistrationIdValue",
DeviceId = "TestDeviceId",
Model = "TestModel",
Platform = "External Web",
Version = "0.0.1-rc1"
});
}
}
catch (Exception ex) {
return View("Index", model: new { error = ex.Message });
}
return View("Index", model: result?.ToString());
2016-07-19 15:57:46 +02:00
}
2016-07-27 10:44:41 +02:00
protected string AccessToken
{
get
{
2016-06-13 13:33:32 +02:00
var claim = HttpContext.User?.FindFirst("access_token");
2016-07-27 10:44:41 +02:00
if (claim == null)
{
2016-06-25 21:03:17 +02:00
throw new InvalidOperationException("no access_token");
2016-06-13 13:33:32 +02:00
}
return claim.Value;
}
}
2016-06-01 23:47:06 +02:00
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
public IActionResult Error()
{
return View();
}
}
}