Mobile Gateway Service provides server-side HTTP signature verification to secure traffic between the gateway and your backend server.
When you enable signature verification for an API group in the gateway console, Mobile Gateway Service signs each API request in that group. You can create the public and private keys used for signing in the gateway console.
Your backend server reads the signature string and performs a local signature calculation on the received request. It then compares the calculated signature with the received signature to determine whether the request is valid.
Reading the signature
The signature calculated by Mobile Gateway Service is stored in the request header. The header key is X-Mgs-Proxy-Signature.
The secret key identifier is configured in the API group and is used to find the corresponding secret value. This identifier is sent in the request header under the key X-Mgs-Proxy-Signature-Secret-Key.
Signature verification methods
Data for adding an organization signature
String stringToSign =
HTTPMethod + "\n" +
Content-MD5 + "\n" +
UrlHTTPMethod: The HTTP method in uppercase, such asPUTorPOST.Content-MD5: The MD5 value of the request body, calculated as follows:If
HTTPMethodis not `PUT` or `POST`, the MD5 value is an empty string (`""`).If the request body is a form, the MD5 is an empty string
"". Otherwise, execute the third step.Calculate the MD5 value. If the request has no body,
bodyStreamis the string"null".String content-MD5 = Base64.encodeBase64(MD5(bodyStream.getBytes("UTF-8")));ImportantEven if the
Content-MD5value is an empty string (`""`), the subsequent line feed (`\n`) in the string to sign must be included. This results in two consecutive `\n` characters in the string.
Url: The `Url` is constructed from the path, query, and form parameters in the body. For example, assume the request ishttp://ip:port/test/testSign?c=3&a=1and the form parameters areb=2&d=4. The `Url` is constructed as follows:Extract the path. The path is the part of the URL after
ip:portand before?. In this example, the path is/test/testSign.If the request has no query or form parameters, the `Url` is the path.
Concatenate the parameters. Sort the query and form parameters by key in lexicographic order. Then, concatenate them into the format
Key1=Value1&Key2=Value2&...&KeyN=ValueN. In this example, the result isa=1&b=2&c=3&d=4.NoteIf a query or form parameter has multiple values, use only the first
value.Construct the `Url`. The format is
Path?Key1=Value1&Key2=Value2&...&KeyN=ValueN. In this example, the `Url` is/test/testSign?a=1&b=2&c=3&d=4.
Verify the signature
Verify the signature using the MD5 algorithm
String sign = "xxxxxxx"; // The signature from Mobile Gateway Service String salt ="xxx"; // The MD5 salt MessageDigest digest = MessageDigest.getInstance("MD5"); String toSignedContent = stringToSign + salt; byte[] content = digest.digest(toSignedContent.getBytes("UTF-8")); String computedSign = new String(Hex.encodeHexString(content)); boolean isSignLegal = sign.equals(computedSign) ? true : false;Verify the signature using the RSA algorithm
String sign = "xxxxxxx"; // The signature from Mobile Gateway Service String publicKey ="xxx"; // The RSA public key from Mobile Gateway Service PublicKey pubKey = KeyReader.getPublicKeyFromX509("RSA", new ByteArrayInputStream(publicKey.getBytes())); java.security.Signature signature = java.security.Signature.getInstance("SHA1WithRSA"); signature.initVerify(pubKey); signature.update(stringToSign.getBytes("UTF-8")); boolean isSignLegal = signature.verify(Base64.decodeBase64(sign.getBytes("UTF-8")));
Code examples
For more information, see HttpSignUtil.java.