Files
rest-samples/templates/template.cs
Nick Alteen fcddb6659e Initial commit of demo issuer creation and permissions update code samples for /wallet documentation
* Added issuer creation and permissions update examples to all templates
* Generated new outputs for each template
* Updated Google SDK version sfor C#, Java, PHP, and Python templates
* General formatting and linting updates
* Additional commenting
* Added stronger typing of variables in C# and Java examples
2022-08-19 13:40:05 -04:00

205 lines
7.5 KiB
C#

/*
* Copyright 2022 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// [START setup]
// [START imports]
using System.IdentityModel.Tokens.Jwt;
using System.Net;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;
using Google.Apis.Auth.OAuth2;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
// [END imports]
/*
* keyFilePath - Path to service account key file from Google Cloud Console
* - Environment variable: GOOGLE_APPLICATION_CREDENTIALS
*/
string keyFilePath = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") ?? "/path/to/key.json";
/*
* issuerId - The issuer ID being used in this request
* - Environment variable: WALLET_ISSUER_ID
*/
string issuerId = Environment.GetEnvironmentVariable("WALLET_ISSUER_ID") ?? "issuer-id";
/*
* classId - Developer-defined ID for the wallet class
* - Environment variable: WALLET_CLASS_ID
*/
string classId = Environment.GetEnvironmentVariable("WALLET_CLASS_ID") ?? "test-$object_type-class-id";
/*
* userId - Developer-defined ID for the user, such as an email address
* - Environment variable: WALLET_USER_ID
*/
string userId = Environment.GetEnvironmentVariable("WALLET_USER_ID") ?? "user-id";
/*
* objectId - ID for the wallet object
* - Format: `issuerId.userId`
* - Should only include alphanumeric characters, '.', '_', or '-'
*/
string objectId = $"{issuerId}.{new Regex(@"[^\w.-]", RegexOptions.Compiled).Replace(userId, "_")}-{classId}";
// [END setup]
///////////////////////////////////////////////////////////////////////////////
// Create authenticated HTTP client, using service account file.
///////////////////////////////////////////////////////////////////////////////
// [START auth]
var credentials = (ServiceAccountCredential)GoogleCredential.FromFile(keyFilePath)
.CreateScoped(new[] { "https://www.googleapis.com/auth/wallet_object.issuer" })
.UnderlyingCredential;
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
await credentials.GetAccessTokenForRequestAsync()
);
// [END auth]
///////////////////////////////////////////////////////////////////////////////
// Create a class via the API (this can also be done in the business console).
///////////////////////////////////////////////////////////////////////////////
// [START class]
string classUrl = "https://walletobjects.googleapis.com/walletobjects/v1/$object_typeClass/";
var classPayload = $class_payload;
HttpRequestMessage classRequest = new HttpRequestMessage(HttpMethod.Post, classUrl);
classRequest.Content = new StringContent(JsonConvert.SerializeObject(classPayload));
HttpResponseMessage classResponse = httpClient.Send(classRequest); ;
string classContent = await classResponse.Content.ReadAsStringAsync();
Console.WriteLine($"class POST response: {classContent}");
// [END class]
///////////////////////////////////////////////////////////////////////////////
// Create an object via the API.
///////////////////////////////////////////////////////////////////////////////
// [START object]
string objectUrl = "https://walletobjects.googleapis.com/walletobjects/v1/$object_typeObject/";
var objectPayload = $object_payload;
HttpRequestMessage objectRequest = new HttpRequestMessage(HttpMethod.Get, $"{objectUrl}{objectId}");
HttpResponseMessage objectResponse = httpClient.Send(objectRequest);
if (objectResponse.StatusCode == HttpStatusCode.NotFound)
{
// Object does not yet exist
// Send POST request to create it
objectRequest = new HttpRequestMessage(HttpMethod.Post, objectUrl);
objectRequest.Content = new StringContent(JsonConvert.SerializeObject(objectPayload));
objectResponse = httpClient.Send(objectRequest);
}
string objectContent = await objectResponse.Content.ReadAsStringAsync();
Console.WriteLine($"object GET or POST response: {objectContent}");
// [END object]
///////////////////////////////////////////////////////////////////////////////
// Create a JWT for the object, and encode it to create a "Save" URL.
///////////////////////////////////////////////////////////////////////////////
// [START jwt]
JwtPayload claims = new JwtPayload();
claims.Add("iss", credentials.Id);
claims.Add("aud", "google");
claims.Add("origins", new string[] { "www.example.com" });
claims.Add("typ", "savetowallet");
claims.Add("payload", new
{
$object_typeObjects = new object[]
{
new
{
id = objectId
}
}
});
RsaSecurityKey key = new RsaSecurityKey(credentials.Key);
SigningCredentials signingCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256);
JwtSecurityToken jwt = new JwtSecurityToken(new JwtHeader(signingCredentials), claims);
string token = new JwtSecurityTokenHandler().WriteToken(jwt);
string saveUrl = $"https://pay.google.com/gp/v/save/{token}";
Console.WriteLine(saveUrl);
// [END jwt]
///////////////////////////////////////////////////////////////////////////////
// Create a new Google Wallet issuer account
///////////////////////////////////////////////////////////////////////////////
// [START createIssuer]
// New issuer name
string issuerName = "name";
// New issuer email address
string issuerEmail = "email-address";
// Issuer API endpoint
string issuerUrl = "https://walletobjects.googleapis.com/walletobjects/v1/issuer";
// New issuer information
var issuerPayload = new
{
name = issuerName,
contactInfo = new
{
email = issuerEmail
}
};
HttpRequestMessage issuerRequest = new HttpRequestMessage(HttpMethod.Post, issuerUrl);
issuerRequest.Content = new StringContent(JsonConvert.SerializeObject(issuerPayload));
HttpResponseMessage issuerResponse = httpClient.Send(issuerRequest);
Console.WriteLine($"issuer POST response: {await issuerResponse.Content.ReadAsStringAsync()}");
// [END createIssuer]
///////////////////////////////////////////////////////////////////////////////
// Update permissions for an existing Google Wallet issuer account
///////////////////////////////////////////////////////////////////////////////
// [START updatePermissions]
// Permissions API endpoint
string permissionsUrl = $"https://walletobjects.googleapis.com/walletobjects/v1/permissions/{issuerId}";
// New issuer permissions information
var permissionsPayload = new
{
issuerId = issuerId,
permissions = new object[]
{
// Copy as needed for each email address that will need access
new
{
emailAddress = "email-address",
role = "READER | WRITER | OWNER"
}
}
};
HttpRequestMessage permissionsRequest = new HttpRequestMessage(HttpMethod.Put, permissionsUrl);
permissionsRequest.Content = new StringContent(JsonConvert.SerializeObject(permissionsPayload));
HttpResponseMessage permissionsResponse = httpClient.Send(permissionsRequest);
Console.WriteLine($"permissions PUT response: {await permissionsResponse.Content.ReadAsStringAsync()}");
// [END updatePermissions]