diff --git a/TestTokenCreator/Controllers/TokenController.cs b/TestTokenCreator/Controllers/TokenController.cs index 339acdb..0ef206e 100644 --- a/TestTokenCreator/Controllers/TokenController.cs +++ b/TestTokenCreator/Controllers/TokenController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using System.Text.Json; using TestTokenCreator.Models; namespace TestTokenCreator.Controllers @@ -36,33 +37,38 @@ namespace TestTokenCreator.Controllers } [HttpGet(Name = "oauth-callback")] - public async Task OAuthCallback() + public async Task OAuthCallback() { - // https://testpatcreation.azurewebsites.net/Token/OAuthCallback?code=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Im9PdmN6NU1fN3AtSGpJS2xGWHo5M3VfVjBabyJ9.eyJhdWkiOiI1MmI2MmJmNC1lOTdhLTQ3ZDctOThhMi03OWViNDEwOTdmOGYiLCJuYW1laWQiOiJjZmQ2Nzg2NC03ZTY5LTZjODMtYTEzNS04YzU4NzNkYTZjODkiLCJzY3AiOiJ2c28uY29kZV9mdWxsIHZzby50b2tlbmFkbWluaXN0cmF0aW9uIHZzby50b2tlbnMgdnNvLmF1dGhvcml6YXRpb25fZ3JhbnQiLCJpc3MiOiJhcHAudnN0b2tlbi52aXN1YWxzdHVkaW8uY29tIiwiYXVkIjoiYXBwLnZzdG9rZW4udmlzdWFsc3R1ZGlvLmNvbSIsIm5iZiI6MTY1MTA2NDEwNywiZXhwIjoxNjUxMDY1MDA3fQ.Aveuz8sKpKBrj1x5Ck8ODUERuOjIIlvJ_3FIUGJMeH4gUTAEAV69YNcIqXzOo2slyt4Kuwy5Ot3-LaiUJFVpM-2OGb3sNvrunMoBccGiJpOBsi-QruabViSMPyVGSRIgP9oj_QrixUK70Q6Jzx5BmIgeay_z0jkuxAkKdlmRVExJ7mDkq_cVFvKmhFBUIB_NFPr20uDfBg0Bi9wra9MUpv4hBeTqia-K65ARmbA0g2EwAt3_na0kkL35dUggVofyjLFQPwcKiUIWOe-RJAKPQTBEoIuHVGrV2mf1fphZ561Bm6QTepMXq47YDibSGlbzr0tJAJ2Z786YbkMgqrJAEg&state=User1 - try { - string code = HttpContext.Request.Query["code"].ToString(); - string state = HttpContext.Request.Query["state"].ToString(); - Console.WriteLine($"Code: {code}"); - Console.WriteLine($"State: {state}"); - - HttpContent content = GenerateRequestPostData(DataModel.Instance.Secret, code, DataModel.Instance.RedirectUri); - HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content); - string resp = await response.Content.ReadAsStringAsync(); - return resp; - } - catch (Exception ex) - { - return "Caught exception : " + ex.Message; - } + string code = HttpContext.Request.Query["code"].ToString(); + string state = HttpContext.Request.Query["state"].ToString(); + Console.WriteLine($"Code: {code}"); + Console.WriteLine($"State: {state}"); + string grantType = "urn:ietf:params:oauth:grant-type:jwt-bearer"; + HttpContent content = GenerateRequestPostData(DataModel.Instance.Secret, grantType, code, DataModel.Instance.RedirectUri); + HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content); + string resp = await response.Content.ReadAsStringAsync(); + TokenModel model = JsonSerializer.Deserialize(resp); + return model; } - public static HttpContent GenerateRequestPostData(string appSecret, string authCode, string callbackUrl) + [HttpGet(Name = "RefreshToken")] + public async Task RefreshToken(string refreshToken) + { + string grantType = "refresh_token"; + HttpContent content = GenerateRequestPostData(DataModel.Instance.Secret, grantType, refreshToken, DataModel.Instance.RedirectUri); + HttpResponseMessage response = await client.PostAsync("https://app.vssps.visualstudio.com/oauth2/token", content); + string resp = await response.Content.ReadAsStringAsync(); + TokenModel model = JsonSerializer.Deserialize(resp); + return model; + } + + public static HttpContent GenerateRequestPostData(string appSecret, string grantType, string authCode, string callbackUrl) { return new FormUrlEncodedContent(new[] { new KeyValuePair("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"), new KeyValuePair("client_assertion", Uri.EscapeUriString(appSecret)), - new KeyValuePair("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"), + new KeyValuePair("grant_type", grantType), new KeyValuePair("assertion", Uri.EscapeUriString(authCode)), new KeyValuePair("redirect_uri", callbackUrl) }); diff --git a/TestTokenCreator/Models/TokenModel.cs b/TestTokenCreator/Models/TokenModel.cs new file mode 100644 index 0000000..d93faf8 --- /dev/null +++ b/TestTokenCreator/Models/TokenModel.cs @@ -0,0 +1,13 @@ +namespace TestTokenCreator.Models +{ + public class TokenModel + { + public string access_token { get; set; } + + public string token_type { get; set; } + + public string expire_in { get; set; } + + public string refresh_token { get; set; } + } +}