92bbba6dc4367c2564605153312da8c38adf10fa
[redakcja.git] /
1 /*\r
2   Copyright (c) 2008, Adobe Systems Incorporated\r
3   All rights reserved.\r
4 \r
5   Redistribution and use in source and binary forms, with or without \r
6   modification, are permitted provided that the following conditions are\r
7   met:\r
8 \r
9   * Redistributions of source code must retain the above copyright notice, \r
10     this list of conditions and the following disclaimer.\r
11   \r
12   * Redistributions in binary form must reproduce the above copyright\r
13     notice, this list of conditions and the following disclaimer in the \r
14     documentation and/or other materials provided with the distribution.\r
15   \r
16   * Neither the name of Adobe Systems Incorporated nor the names of its \r
17     contributors may be used to endorse or promote products derived from \r
18     this software without specific prior written permission.\r
19 \r
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
21   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
22   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
23   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
24   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
25   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
26   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
27   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
28   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
29   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
30   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
31 */\r
32 \r
33 package com.adobe.crypto\r
34 {\r
35         import mx.formatters.DateFormatter;\r
36         import mx.utils.Base64Encoder;\r
37         \r
38         /**\r
39          * Web Services Security Username Token\r
40          *\r
41          * Implementation based on algorithm description at \r
42          * http://www.oasis-open.org/committees/wss/documents/WSS-Username-02-0223-merged.pdf\r
43          */\r
44         public class WSSEUsernameToken\r
45         {\r
46                 /**\r
47                  * Generates a WSSE Username Token.\r
48                  *\r
49                  * @param username The username\r
50                  * @param password The password\r
51                  * @param nonce A cryptographically random nonce (if null, the nonce\r
52                  * will be generated)\r
53                  * @param timestamp The time at which the token is generated (if null,\r
54                  * the time will be set to the moment of execution)\r
55                  * @return The generated token\r
56                  * @langversion ActionScript 3.0\r
57                  * @playerversion Flash 9.0\r
58                  * @tiptext\r
59                  */\r
60                 public static function getUsernameToken(username:String, password:String, nonce:String=null, timestamp:Date=null):String\r
61                 {\r
62                         if (nonce == null)\r
63                         {\r
64                                 nonce = generateNonce();\r
65                         }\r
66                         nonce = base64Encode(nonce);\r
67                 \r
68                         var created:String = generateTimestamp(timestamp);\r
69                 \r
70                         var password64:String = getBase64Digest(nonce,\r
71                                 created,\r
72                                 password);\r
73                 \r
74                         var token:String = new String("UsernameToken Username=\"");\r
75                         token += username + "\", " +\r
76                                          "PasswordDigest=\"" + password64 + "\", " +\r
77                                          "Nonce=\"" + nonce + "\", " +\r
78                                          "Created=\"" + created + "\"";\r
79                         return token;\r
80                 }\r
81                 \r
82                 private static function generateNonce():String\r
83                 {\r
84                         // Math.random returns a Number between 0 and 1.  We don't want our\r
85                         // nonce to contain invalid characters (e.g. the period) so we\r
86                         // strip them out before returning the result.\r
87                         var s:String =  Math.random().toString();\r
88                         return s.replace(".", "");\r
89                 }\r
90                 \r
91                 internal static function base64Encode(s:String):String\r
92                 {\r
93                         var encoder:Base64Encoder = new Base64Encoder();\r
94                         encoder.encode(s);\r
95                         return encoder.flush();\r
96                 }\r
97                 \r
98                 internal static function generateTimestamp(timestamp:Date):String\r
99                 {\r
100                         if (timestamp == null)\r
101                         {\r
102                                 timestamp = new Date();\r
103                         }\r
104                         var dateFormatter:DateFormatter = new DateFormatter();\r
105                         dateFormatter.formatString = "YYYY-MM-DDTJJ:NN:SS"\r
106                         return dateFormatter.format(timestamp) + "Z";\r
107                 }\r
108                 \r
109                 internal static function getBase64Digest(nonce:String, created:String, password:String):String\r
110                 {\r
111                         return SHA1.hashToBase64(nonce + created + password);\r
112                 }\r
113         }\r
114 }