Example #1
0
        public IActionResult Init([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "init")] HttpRequest req)
        {
            var clientId = Config.GetValue <string>("VismaNetClientId") ??
                           throw new InvalidArgumentsException("client-id is missing");
            var callbackUrl = Config.GetValue <string>("VismaNetCallbackUrl") ??
                              throw new InvalidArgumentsException("VismaNetCallbackUrl is missing");
            var redirectUrl = VismaNet.GetOAuthUrl(clientId, callbackUrl);

            return(new RedirectResult(redirectUrl));
        }
Example #2
0
        public static async Task <IActionResult> Callback(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "callback")]
            HttpRequest req,
            [Queue(SendGridQueue)] IAsyncCollector <string> emailQueue,
            ILogger log)
        {
            var clientId = Config.GetValue <string>("VismaNetClientId") ??
                           throw new InvalidArgumentsException("VismaNetClientId is missing");
            var clientSecret = Config.GetValue <string>("VismaNetClientSecret") ??
                               throw new InvalidArgumentsException("VismaNetClientSecret is missing");
            var callbackUrl = Config.GetValue <string>("VismaNetCallbackUrl") ??
                              throw new InvalidArgumentsException("VismaNetCallbackUrl is missing");
            var code = req.GetQueryParameterDictionary()["code"];

            try
            {
                var token = await VismaNet.GetTokenUsingOAuth(clientId, clientSecret, code, callbackUrl);

                var contexts = await VismaNet.GetContextsForToken(token);

                var builder = new StringBuilder();
                builder.AppendLine($"<pre><strong>Token:</strong> {token}</p>");
                builder.AppendLine("<p><strong>Available contexts</strong></p>");
                builder.AppendLine("<ul>");
                foreach (var ctx in contexts)
                {
                    builder.AppendLine($"<li>{ctx.name} ({ctx.id})</li>");
                }

                builder.AppendLine("</ul>");
                try
                {
                    dynamic vismaNet = new VismaNet(contexts.First().id, token);
                    var     userInfo = await vismaNet.context.userdetails.All();

                    var user = userInfo[0];
                    builder.AppendLine(
                        $"<p>Created by {user.firstName} {user.lastName} (<a href='mailto:{user.emailAddress}'>{user.emailAddress}</a>)</p>");
                }
                catch (Exception e)
                {
                    log.LogError(e, $"Could not fetch user details. {e.Message}");
                }

                if (!string.IsNullOrEmpty(Config.GetValue <string>("AzureWebJobsSendGridApiKey")))
                {
                    await emailQueue.AddAsync(builder.ToString());

                    return(CreateTemplatedResult("Token created successfully",
                                                 "<p>A token was generated and sent to us.</p>", background: "green darken-2"));
                }

                return(CreateTemplatedResult("Token created successfully", $"<p>{builder}</p>",
                                             background: "green darken-2"));
            }
            catch (Exception e)
            {
                log.LogError(e, e.Message);
                return(CreateTemplatedResult(
                           "<i class='material-icons medium right'>warning</i> Unable to create token", $"<p>{e.Message}</p>",
                           HttpStatusCode.InternalServerError, "red darken-2"));
            }
        }