source: code/Website/open-flash-chart/com/adobe/crypto/WSSEUsernameToken.as@ 7849

Last change on this file since 7849 was 7849, checked in by dennisw, 15 years ago
File size: 4.2 KB
Line 
1/*
2Adobe Systems Incorporated(r) Source Code License Agreement
3Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
4
5Please read this Source Code License Agreement carefully before using
6the source code.
7
8Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
9no-charge, royalty-free, irrevocable copyright license, to reproduce,
10prepare derivative works of, publicly display, publicly perform, and
11distribute this source code and such derivative works in source or
12object code form without any attribution requirements.
13
14The name "Adobe Systems Incorporated" must not be used to endorse or promote products
15derived from the source code without prior written permission.
16
17You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
18against any loss, damage, claims or lawsuits, including attorney's
19fees that arise or result from your use or distribution of the source
20code.
21
22THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
23ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
24BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
26NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
27OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
33ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36package com.adobe.crypto
37{
38 import mx.formatters.DateFormatter;
39 import mx.utils.Base64Encoder;
40
41 /**
42 * Web Services Security Username Token
43 *
44 * Implementation based on algorithm description at
45 * http://www.oasis-open.org/committees/wss/documents/WSS-Username-02-0223-merged.pdf
46 */
47 public class WSSEUsernameToken
48 {
49 /**
50 * Generates a WSSE Username Token.
51 *
52 * @param username The username
53 * @param password The password
54 * @param nonce A cryptographically random nonce (if null, the nonce
55 * will be generated)
56 * @param timestamp The time at which the token is generated (if null,
57 * the time will be set to the moment of execution)
58 * @return The generated token
59 * @langversion ActionScript 3.0
60 * @playerversion Flash 9.0
61 * @tiptext
62 */
63 public static function getUsernameToken(username:String, password:String, nonce:String=null, timestamp:Date=null):String
64 {
65 if (nonce == null)
66 {
67 nonce = generateNonce();
68 }
69 nonce = base64Encode(nonce);
70
71 var created:String = generateTimestamp(timestamp);
72
73 var password64:String = getBase64Digest(nonce,
74 created,
75 password);
76
77 var token:String = new String("UsernameToken Username=\"");
78 token += username + "\", " +
79 "PasswordDigest=\"" + password64 + "\", " +
80 "Nonce=\"" + nonce + "\", " +
81 "Created=\"" + created + "\"";
82 return token;
83 }
84
85 private static function generateNonce():String
86 {
87 // Math.random returns a Number between 0 and 1. We don't want our
88 // nonce to contain invalid characters (e.g. the period) so we
89 // strip them out before returning the result.
90 var s:String = Math.random().toString();
91 return s.replace(".", "");
92 }
93
94 internal static function base64Encode(s:String):String
95 {
96 var encoder:Base64Encoder = new Base64Encoder();
97 encoder.encode(s);
98 return encoder.flush();
99 }
100
101 internal static function generateTimestamp(timestamp:Date):String
102 {
103 if (timestamp == null)
104 {
105 timestamp = new Date();
106 }
107 var dateFormatter:DateFormatter = new DateFormatter();
108 dateFormatter.formatString = "YYYY-MM-DDTJJ:NN:SS"
109 return dateFormatter.format(timestamp) + "Z";
110 }
111
112 internal static function getBase64Digest(nonce:String, created:String, password:String):String
113 {
114 return SHA1.hashToBase64(nonce + created + password);
115 }
116 }
117}
Note: See TracBrowser for help on using the repository browser.