0
votes

I'm trying to create an Authentication function for user login, but my idea is to expose the "function keys" of the rest of the functions. So the mobile app can grave the keys to star calling the rest of the functions.

Is a way to do this?

1
what is 'function key' in your understanding? why not to use authentication via JWT token or cookie, for example? "grave the keys to star" what does it suppose to mean?Yegor Androsov
the Function key is the KEY at the AuthorizationLevel.Function As far as I understand I can not use JWT tokens like a netcore WebAPI. Actually my current WebAPI is using JWT tokens and that's why I'm looking for an alternative for this new implementation with Azure FunctionsDanielRamiz
technically, you can get request headers and validate token yourself. Not sure what is status for User authentication feature though. github.com/Azure/azure-functions-host/issues/33Yegor Androsov
Yes, I saw some examples to validate the token, but never how to create the token. do you have any examples for Azure Functions?DanielRamiz
@DanielRamiz you need to perform operations on function keys am i right ?HariHaran

1 Answers

0
votes

If you want to manage Azure function key, you can use the Key management API to implement it. For more details, please refer to document

  1. Get function key
GET https://<functionappname>.azurewebsites.net/admin/functions/{functionname}/keys

  1. Create Function key
PUT https://<functionappname>.azurewebsites.net/admin/functions/{functionname}/keys/{keyname}

{
  "name": "keyname",
  "value" : "keyvalue"
}

The code

tring clientId = "client id";
     string secret = "secret key";
     string tenant = "tenant id";
     var functionName ="functionName";
     var webFunctionAppName = "functionApp name";
     string resourceGroup = "resource group name";
     var credentials = new AzureCredentials(new ServicePrincipalLoginInformation { ClientId = clientId, ClientSecret = secret}, tenant, AzureEnvironment.AzureGlobalCloud);
     var azure = Azure
              .Configure()
              .Authenticate(credentials)
              .WithDefaultSubscription();

     var webFunctionApp = azure.AppServices.FunctionApps.GetByResourceGroup(resourceGroup, webFunctionAppName);
     var ftpUsername = webFunctionApp.GetPublishingProfile().FtpUsername;
     var username = ftpUsername.Split('\\').ToList()[1];
     var password = webFunctionApp.GetPublishingProfile().FtpPassword;
     var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
     var apiUrl = new Uri($"https://{webFunctionAppName}.scm.azurewebsites.net/api");
     var siteUrl = new Uri($"https://{webFunctionAppName}.azurewebsites.net");
     string JWT;
     using (var client = new HttpClient())
      {
         client.DefaultRequestHeaders.Add("Authorization", $"Basic {base64Auth}");

         var result = client.GetAsync($"{apiUrl}/functions/admin/token").Result;
         JWT = result.Content.ReadAsStringAsync().Result.Trim('"'); //get  JWT for call funtion key
       }
// get key
     using (var client = new HttpClient())
     {
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + JWT);
        var key = await client.GetAsync($"{siteUrl}/admin/functions/{functionName}/keys").Result.Content.ReadAsStringAsync();
      }

// create key
var map = new Dictionary<string, string>();
            map.Add("name", "keyName");
            map.Add("value", "keyVaule");


            using (var client = new HttpClient()) {

                var content = new StringContent(JsonConvert.SerializeObject(map), System.Text.Encoding.UTF8, "application/json");
                await client.PutAsync($"{siteUrl}/admin/functions/{functionname}/keys/{keyname}", content);


            }

Besides, according to my research, we also can use Azure REST API to manage Azure function key. For more details, please refer to

a. Create Azure function key

b. List Azure function key