/// <summary> /// Initializes the object with an X509 certificate /// </summary> private void Initialize(X509Certificate2 certificate) { X509IdentityToken token = new X509IdentityToken(); token.CertificateData = certificate.RawData; token.Certificate = certificate; Initialize(token); }
/// <summary> /// Initializes the object with an X509 certificate /// </summary> public UserIdentity(X509Certificate2 certificate) { if (certificate == null) { throw new ArgumentNullException("certificate"); } X509IdentityToken token = new X509IdentityToken(); token.CertificateData = certificate.RawData; token.Certificate = certificate; Initialize(token); }
/// <summary> /// Initializes the object with a UA identity token /// </summary> private void Initialize(UserIdentityToken token) { if (token == null) { throw new ArgumentNullException("token"); } m_policyId = token.PolicyId; UserNameIdentityToken usernameToken = token as UserNameIdentityToken; if (usernameToken != null) { Initialize(new UserNameSecurityToken(usernameToken.UserName, usernameToken.DecryptedPassword)); return; } X509IdentityToken x509Token = token as X509IdentityToken; if (x509Token != null) { X509Certificate2 certificate = CertificateFactory.Create(x509Token.CertificateData, true); Initialize(new X509SecurityToken(certificate)); return; } IssuedIdentityToken wssToken = token as IssuedIdentityToken; if (wssToken != null) { Initialize(wssToken, WSSecurityTokenSerializer.DefaultInstance, null); return; } AnonymousIdentityToken anonymousToken = token as AnonymousIdentityToken; if (anonymousToken != null) { m_tokenType = UserTokenType.Anonymous; m_issuedTokenType = null; m_displayName = "Anonymous"; m_token = null; return; } throw new ArgumentException("Unrecognized UA user identity token type.", "token"); }
/// <summary> /// Initializes the object with an X509 certificate identifier /// </summary> public UserIdentity(CertificateIdentifier certificateId) { if (certificateId == null) { throw new ArgumentNullException("certificateId"); } Task <X509Certificate2> task = certificateId.Find(); task.Wait(); X509Certificate2 certificate = task.Result; if (certificate != null) { X509IdentityToken token = new X509IdentityToken(); token.CertificateData = certificate.RawData; token.Certificate = certificate; Initialize(token); } }
/// <summary> /// Initializes the object with a UA identity token /// </summary> private void Initialize(UserIdentityToken token) { if (token == null) { throw new ArgumentNullException("token"); } m_token = token; UserNameIdentityToken usernameToken = token as UserNameIdentityToken; if (usernameToken != null) { m_tokenType = UserTokenType.UserName; m_issuedTokenType = null; m_displayName = usernameToken.UserName; return; } X509IdentityToken x509Token = token as X509IdentityToken; if (x509Token != null) { m_tokenType = UserTokenType.Certificate; m_issuedTokenType = null; if (x509Token.Certificate != null) { m_displayName = x509Token.Certificate.Subject; } else { X509Certificate2 cert = CertificateFactory.Create(x509Token.CertificateData, true); m_displayName = cert.Subject; } return; } IssuedIdentityToken issuedToken = token as IssuedIdentityToken; if (issuedToken != null) { if (issuedToken.IssuedTokenType == Ua.IssuedTokenType.JWT) { if (issuedToken.DecryptedTokenData == null || issuedToken.DecryptedTokenData.Length == 0) { throw new ArgumentException("JSON Web Token has no data associated with it.", "token"); } m_tokenType = UserTokenType.IssuedToken; m_issuedTokenType = new XmlQualifiedName("", Opc.Ua.Profiles.JwtUserToken); m_displayName = "JWT"; return; } else { throw new NotSupportedException("Only JWT Issued Tokens are supported!"); } } AnonymousIdentityToken anonymousToken = token as AnonymousIdentityToken; if (anonymousToken != null) { m_tokenType = UserTokenType.Anonymous; m_issuedTokenType = null; m_displayName = "Anonymous"; return; } throw new ArgumentException("Unrecognized UA user identity token type.", "token"); }
/// <summary cref="IUserIdentity.GetIdentityToken" /> public UserIdentityToken GetIdentityToken() { // check for anonymous. if (m_token == null) { AnonymousIdentityToken token = new AnonymousIdentityToken(); token.PolicyId = m_policyId; return(token); } // return a user name token. UserNameSecurityToken usernameToken = m_token as UserNameSecurityToken; if (usernameToken != null) { UserNameIdentityToken token = new UserNameIdentityToken(); token.PolicyId = m_policyId; token.UserName = usernameToken.UserName; token.DecryptedPassword = usernameToken.Password; return(token); } // return an X509 token. X509SecurityToken x509Token = m_token as X509SecurityToken; if (x509Token != null) { X509IdentityToken token = new X509IdentityToken(); token.PolicyId = m_policyId; token.CertificateData = x509Token.Certificate.GetRawCertData(); token.Certificate = x509Token.Certificate; return(token); } // handle SAML token. SamlSecurityToken samlToken = m_token as SamlSecurityToken; if (samlToken != null) { MemoryStream ostrm = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(ostrm, new UTF8Encoding()); try { SamlSerializer serializer = new SamlSerializer(); serializer.WriteToken(samlToken, writer, WSSecurityTokenSerializer.DefaultInstance); } finally { writer.Close(); } IssuedIdentityToken wssToken = new IssuedIdentityToken(); wssToken.PolicyId = m_policyId; wssToken.DecryptedTokenData = ostrm.ToArray(); return(wssToken); } // return a WSS token by default. if (m_token != null) { MemoryStream ostrm = new MemoryStream(); XmlWriter writer = new XmlTextWriter(ostrm, new UTF8Encoding()); try { WSSecurityTokenSerializer serializer = new WSSecurityTokenSerializer(); serializer.WriteToken(writer, m_token); } finally { writer.Close(); } IssuedIdentityToken wssToken = new IssuedIdentityToken(); wssToken.PolicyId = m_policyId; wssToken.DecryptedTokenData = ostrm.ToArray(); return(wssToken); } return(null); }
/// <summary cref="IUserIdentity.GetIdentityToken" /> public UserIdentityToken GetIdentityToken() { // check for anonymous. if (m_token == null) { AnonymousIdentityToken token = new AnonymousIdentityToken(); token.PolicyId = m_policyId; return token; } // return a user name token. UserNameSecurityToken usernameToken = m_token as UserNameSecurityToken; if (usernameToken != null) { UserNameIdentityToken token = new UserNameIdentityToken(); token.PolicyId = m_policyId; token.UserName = usernameToken.UserName; token.DecryptedPassword = usernameToken.Password; return token; } // return an X509 token. X509SecurityToken x509Token = m_token as X509SecurityToken; if (x509Token != null) { X509IdentityToken token = new X509IdentityToken(); token.PolicyId = m_policyId; token.CertificateData = x509Token.Certificate.GetRawCertData(); token.Certificate = x509Token.Certificate; return token; } // handle SAML token. SamlSecurityToken samlToken = m_token as SamlSecurityToken; if (samlToken != null) { MemoryStream ostrm = new MemoryStream(); XmlTextWriter writer = new XmlTextWriter(ostrm, new UTF8Encoding()); try { SamlSerializer serializer = new SamlSerializer(); serializer.WriteToken(samlToken, writer, WSSecurityTokenSerializer.DefaultInstance); } finally { writer.Close(); } IssuedIdentityToken wssToken = new IssuedIdentityToken(); wssToken.PolicyId = m_policyId; wssToken.DecryptedTokenData = ostrm.ToArray(); return wssToken; } // return a WSS token by default. if (m_token != null) { MemoryStream ostrm = new MemoryStream(); XmlWriter writer = new XmlTextWriter(ostrm, new UTF8Encoding()); try { WSSecurityTokenSerializer serializer = new WSSecurityTokenSerializer(); serializer.WriteToken(writer, m_token); } finally { writer.Close(); } IssuedIdentityToken wssToken = new IssuedIdentityToken(); wssToken.PolicyId = m_policyId; wssToken.DecryptedTokenData = ostrm.ToArray(); return wssToken; } return null; }
/// <summary> /// Initializes the object with an X509 certificate /// </summary> private void Initialize(X509Certificate2 certificate) { X509IdentityToken token = new X509IdentityToken(); token.CertificateData = certificate.RawData; token.Certificate = certificate; Initialize(token); }