Skip to content

Commit

Permalink
Fixes #1140 - Ensure UserTokenPolicy.policyId is unique within the co…
Browse files Browse the repository at this point in the history
…ntext of the server. (#1357)

Fix #1140
  • Loading branch information
AlinMoldovean committed Apr 13, 2021
1 parent 7b20fcb commit 901a171
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions Stack/Opc.Ua.Core/Stack/Server/ServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ ICertificateValidator certificateValidator
/// <returns>Returns a collection of UserTokenPolicy objects, the return type is <seealso cref="UserTokenPolicyCollection"/> . </returns>
public virtual UserTokenPolicyCollection GetUserTokenPolicies(ApplicationConfiguration configuration, EndpointDescription description)
{
int policyId = 0;
UserTokenPolicyCollection policies = new UserTokenPolicyCollection();

if (configuration.ServerConfiguration == null || configuration.ServerConfiguration.UserTokenPolicies == null)
Expand All @@ -764,28 +765,30 @@ public virtual UserTokenPolicyCollection GetUserTokenPolicies(ApplicationConfigu

foreach (UserTokenPolicy policy in configuration.ServerConfiguration.UserTokenPolicies)
{
// ensure a security policy is specified for user tokens.
if (description.SecurityMode == MessageSecurityMode.None)
UserTokenPolicy clone = (UserTokenPolicy)policy.MemberwiseClone();

if (String.IsNullOrEmpty(policy.SecurityPolicyUri))
{
if (String.IsNullOrEmpty(policy.SecurityPolicyUri))
// ensure each policy has a unique id.
if (description.SecurityMode == MessageSecurityMode.None)
{
UserTokenPolicy clone = (UserTokenPolicy)policy.MemberwiseClone();
// ensure a security policy is specified for user tokens.
clone.SecurityPolicyUri = SecurityPolicies.Basic256;
policies.Add(clone);
continue;
clone.PolicyId = Utils.Format("{0}", ++policyId);
}
else
{
clone.PolicyId = Utils.Format("{0}", policyId++);
}
}

policies.Add(policy);
}

// ensure each policy has a unique id.
for (int ii = 0; ii < policies.Count; ii++)
{
if (String.IsNullOrEmpty(policies[ii].PolicyId))
policyId++;
}
else
{
policies[ii].PolicyId = Utils.Format("{0}", ii);
clone.PolicyId = Utils.Format("{0}", policyId++);
}

policies.Add(clone);
}

return policies;
Expand Down

0 comments on commit 901a171

Please sign in to comment.