forked from jbufu/openid4java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenIDToken.java
More file actions
184 lines (156 loc) · 5.35 KB
/
OpenIDToken.java
File metadata and controls
184 lines (156 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright 2006-2008 Sxip Identity Corporation
*/
package org.openid4java.infocard;
import org.openid4java.message.Message;
import org.openid4java.message.ParameterList;
import org.openid4java.OpenIDException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
* Models the OpenID Infocard token used to transport OpenID messages.
* An OpenID token encapsulates an OpenID message in key-value form into
* an <openid:OpenIDToken> element.
* <p>
* Provides functionality for OPs / Servers to create OpenID tokens from
* OpenID messages, and for RPs / Consumers to parse received tokens into
* OpenID messages.
*/
public class OpenIDToken
{
private static Log _log = LogFactory.getLog(OpenIDToken.class);
private static final boolean DEBUG = _log.isDebugEnabled();
/**
* Token type data structure.
*/
private OpenIDTokenType _tokenType;
/**
* The encapsulated OpenID Message.
*/
private Message _openidMessage;
/**
* Constructs an OpenID token encapsulating the provided OpenID Message.
* Should be used on the OP/STS side to generate a RSTR.
*
* @param openidMessage The OpenID message obtained from
* ServerManager.authResponse().
*/
public OpenIDToken(Message openidMessage)
{
setOpenIDMessage(openidMessage);
if (DEBUG)
_log.debug("Created " + _tokenType +" token");
}
/**
* Parses the data posted by the selector into an OpenID token.
* Should be used on the RP side.
*
* @param xmlToken The "xmlToken" parameter posted by the selector.
* @return An OpenIDToken encapsulating the OpenID AuthResponse.
*/
public static OpenIDToken createFromXmlToken(String xmlToken)
throws InfocardException
{
if (xmlToken == null)
throw new InfocardException("Error processing xmlToken: null value");
if (DEBUG)
_log.debug("Processing xmlToken: " + xmlToken);
try
{
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(
new ByteArrayInputStream(xmlToken.getBytes("utf-8")));
String keyValueForm;
try
{
keyValueForm = document.getElementsByTagNameNS(
Message.OPENID2_NS, "OpenIDToken")
.item(0).getFirstChild().getNodeValue();
}
catch (Exception e)
{
throw new InfocardException(
"Error extracting OpenID message from the xmlToken", e);
}
Message message = Message.createMessage(
ParameterList.createFromKeyValueForm(keyValueForm));
return new OpenIDToken(message);
// DOM exceptions :
}
catch (ParserConfigurationException e)
{
throw new InfocardException("Parser configuration error", e);
}
catch (SAXException e)
{
throw new InfocardException("Error parsing XML token document", e);
}
catch (IOException e)
{
throw new InfocardException("Error reading xmlToken document", e);
}
catch (OpenIDException e)
{
throw new InfocardException("Error building OpenID message from xmlToken", e);
}
}
/**
* Gets the OpenID message contained in the OpenID token.
*/
public Message getOpenIDMessage()
{
return _openidMessage;
}
/**
* Gets the OpenID message as a ParameterList.
* @return ParameterList containing the OpenID message.
*/
public ParameterList getOpenIDParams()
{
return new ParameterList(_openidMessage.getParameterMap());
}
/**
* Sets the OpenID Message to encapsulate into the token.
*/
public void setOpenIDMessage(Message openidMessage)
{
this._openidMessage = openidMessage;
if (OpenIDTokenType.OPENID20_TOKEN.toString().equals(
openidMessage.getParameterValue("openid.ns")))
_tokenType = OpenIDTokenType.OPENID20_TOKEN;
else
_tokenType = OpenIDTokenType.OPENID11_TOKEN;
}
/**
* Gets the OpenID token type.
*
* @see org.openid4java.infocard.OpenIDTokenType
*/
public OpenIDTokenType getTokenType()
{
return _tokenType;
}
/**
* Generates the XML string representation of the OpenID token.
*/
public String getToken()
{
StringBuffer token = new StringBuffer();
token.append("<openid:OpenIDToken xmlns:openid=\"" +
Message.OPENID2_NS + "\">");
token.append(_openidMessage.keyValueFormEncoding());
token.append("</openid:OpenIDToken>");
return token.toString();
}
}