1 | /*
|
---|
2 | Adobe Systems Incorporated(r) Source Code License Agreement
|
---|
3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
---|
4 |
|
---|
5 | Please read this Source Code License Agreement carefully before using
|
---|
6 | the source code.
|
---|
7 |
|
---|
8 | Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
|
---|
9 | no-charge, royalty-free, irrevocable copyright license, to reproduce,
|
---|
10 | prepare derivative works of, publicly display, publicly perform, and
|
---|
11 | distribute this source code and such derivative works in source or
|
---|
12 | object code form without any attribution requirements.
|
---|
13 |
|
---|
14 | The name "Adobe Systems Incorporated" must not be used to endorse or promote products
|
---|
15 | derived from the source code without prior written permission.
|
---|
16 |
|
---|
17 | You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
|
---|
18 | against any loss, damage, claims or lawsuits, including attorney's
|
---|
19 | fees that arise or result from your use or distribution of the source
|
---|
20 | code.
|
---|
21 |
|
---|
22 | THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
|
---|
23 | ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
|
---|
24 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
---|
25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
|
---|
26 | NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
|
---|
27 | OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
28 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
29 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
---|
30 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
---|
31 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
---|
32 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
|
---|
33 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
34 | */
|
---|
35 |
|
---|
36 | package 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 | }
|
---|