|
| 1 | +using Frank.Scheduler.Client.Configuration; |
| 2 | +using RestSharp; |
| 3 | +using System; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Frank.Scheduler.Client.Rest |
| 7 | +{ |
| 8 | + public class AuthenticatorService |
| 9 | + { |
| 10 | + public AccessToken GetToken(SchedulerRestConfiguration restConfiguration, AuthenticationOptions authenticationOptions) |
| 11 | + { |
| 12 | + var base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(authenticationOptions.ClientId + ":" + authenticationOptions.ClientSecret)); |
| 13 | + var client = new RestClient(authenticationOptions.BaseUrl); |
| 14 | + client.Timeout = 30; |
| 15 | + var request = new RestRequest(Method.POST); |
| 16 | + request.AddHeader("Authorization", $"Basic {base64String}"); |
| 17 | + request.AlwaysMultipartFormData = true; |
| 18 | + request.AddParameter("grant_type", "password"); |
| 19 | + request.AddParameter("scope", authenticationOptions.Scope); |
| 20 | + request.AddParameter("username", authenticationOptions.UserName); |
| 21 | + request.AddParameter("password", authenticationOptions.Password); |
| 22 | + var response = client.Execute<AccessToken>(request); |
| 23 | + |
| 24 | + return response.Data; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public class AccessToken |
| 29 | + { |
| 30 | + public string? Token { get; set; } |
| 31 | + |
| 32 | + public string? TokenType { get; set; } |
| 33 | + |
| 34 | + public string? Scope { get; set; } |
| 35 | + } |
| 36 | + |
| 37 | + public class AuthenticationOptions |
| 38 | + { |
| 39 | + public AuthenticationOptions(string baseUrl, string userName, string password, string companyId, string clientId, string clientSecret, string scope) |
| 40 | + { |
| 41 | + BaseUrl = baseUrl; |
| 42 | + UserName = userName; |
| 43 | + Password = password; |
| 44 | + CompanyId = companyId; |
| 45 | + ClientId = clientId; |
| 46 | + ClientSecret = clientSecret; |
| 47 | + Scope = scope; |
| 48 | + } |
| 49 | + |
| 50 | + public string? BaseUrl; |
| 51 | + public string? UserName; |
| 52 | + public string? Password; |
| 53 | + public string? CompanyId; |
| 54 | + public string? ClientId; |
| 55 | + public string? ClientSecret; |
| 56 | + public string? Scope; |
| 57 | + } |
| 58 | +} |
0 commit comments