#!/usr/bin/env python
#
# Copyright 2007 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.
#




"""App identity stub service implementation.

This service behaves the same as the production service, except using
constant values instead of app-specific values:
* Signing key is constant; in production this rotates.
* Public key is constant; in production this varies per app.
* Service account name is constant; in production this varies per app.
"""








import binascii

try:
  from Crypto.Hash import SHA256
  from Crypto.PublicKey import RSA
  from Crypto.Util import number
  CRYPTO_LIB_INSTALLED = True
except ImportError, e:
  CRYPTO_LIB_INSTALLED = False

from google.appengine.api import apiproxy_stub

APP_SERVICE_ACCOUNT_NAME = 'test@localhost'

SIGNING_KEY_NAME = 'key'


N = 19119371788959611760073322421014045870056498252163411380847152703712917776733759011400972099255719579701566470175077491500050513917658074590935646529525468755348555932670175295728802986097707368373781743941167574738113348515272061138933984990014969297930973127363812200790406743271047572192133912023914306041356562363557723417403707408838823620411045628159183655215061768071407845537324145892973481372872161981015237572556138317222082306397041309823528068650373958169977675424007883635551170458356632131122901683151395297447872184074888239102348331222079943386530179883880518236689216575776729057173406091195993394637
MODULUS_BYTES = 256

E = 65536L

D = 16986504444572720056487621821047100642841595850137583213470349776864799280835251113078612103869013355016302383270733509621770011190160658118800356360958694229960556902751935956316359959542321272425222634888969943798180994410031448370776358545990991384123912313866752051562052322103544805811361355593091450379904792608637886965065110019212136239200637553477192566763015004249754677600683846556806159369233241157779976231822757855748068765507787598014034587835400718727569389998321277712761796543890788269130617890866139616903097422259980026836628018133574943835504630997228592718738382001678104796538128020421537193913
X509_PUBLIC_CERT = """
-----BEGIN CERTIFICATE-----
MIIC/jCCAeagAwIBAgIIQTBFcRw3moMwDQYJKoZIhvcNAQEFBQAwIjE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
HEADER2 = '00'
PADDING = 'ff'


LENGTH_OF_SHA256_HASH = 32


class AppIdentityServiceStub(apiproxy_stub.APIProxyStub):
  """A stub for the AppIdentityService API for offline development.

  Provides stub functions which allow a developer to test integration before
  deployment.
  """
  def __init__(self, service_name='app_identity_service'):
    """Constructor."""
    super(AppIdentityServiceStub, self).__init__(service_name)

  def _Dynamic_SignForApp(self, request, response):
    """Implementation of AppIdentityService::SignForApp."""
    if not CRYPTO_LIB_INSTALLED:
      raise NotImplementedError("""Unable to import the pycrypto module,
                                SignForApp is disabled.""")
    rsa_obj = RSA.construct((N, E, D))
    hashObj = SHA256.new()
    hashObj.update(request.bytes_to_sign())
    padding_length = MODULUS_BYTES - LEN_OF_PREFIX - LENGTH_OF_SHA256_HASH - 3
    emsa = (HEADER1 + (PADDING * padding_length) + HEADER2 +
            PREFIX + hashObj.hexdigest())
    sig = rsa_obj.sign(binascii.a2b_hex(emsa), '')
    response.set_signature_bytes(number.long_to_bytes(sig[0]))
    response.set_key_name(SIGNING_KEY_NAME)

  def _Dynamic_GetPublicCertificatesForApp(self, request, response):
    """Implementation of AppIdentityService::GetPublicCertificatesForApp"""
    cert = response.add_public_certificate_list()
    cert.set_key_name(SIGNING_KEY_NAME)
    cert.set_x509_certificate_pem(X509_PUBLIC_CERT)

  def _Dynamic_GetServiceAccountName(self, request, response):
    """Implementation of AppIdentityService::GetServiceAccountName"""
    response.set_service_account_name(APP_SERVICE_ACCOUNT_NAME)
