public override void Add(int accountId, IUser user) { if (string.IsNullOrEmpty(user.Name)) { throw new UsersException(ErrorCodes.UsernameEmpty); } var sync = GetSync(accountId); sync.EnterWriteLock(); try { var users = accounts.GetOrAdd(accountId, NewAccount); if (users.ContainsKey(user.Name)) { throw new UsersException(ErrorCodes.UserExist); } users.Add(user.Name, new CsvUser(user)); var fileName = files.GetFileName(accountId); if (Directory.Exists(Path.GetDirectoryName(fileName)) == false) { Directory.CreateDirectory(Path.GetDirectoryName(fileName)); } File.AppendAllText(fileName, "\r\n" + CsvUser.ToString(user)); } finally { sync.ExitWriteLock(); } }
private Publication GetValidPublication(string publicationId, bool addIfNotFound) { Publication publication; if (publications.TryGetValue(publicationId, out publication) == false) { if (addIfNotFound) { publication = publications.GetOrAdd(publicationId, new Publication(publicationId)); } } return(publication); }
public ErrorCodes Authorize(SipMessageReader reader, AuthSchemes scheme, out ArraySegment <byte> token, out int opaque, out bool proxy) { token = new ArraySegment <byte>(); //var credentials = reader.GetCredentialsByTargetname(scheme, targetname, out proxy); IAccount account; var credentials = FindCredentials(reader, out account, out proxy); if (HexEncoding.TryParseHex8(credentials.Opaque, out opaque) == false) { opaque = Interlocked.Increment(ref opaqueCount); } if (credentials.AuthScheme == AuthSchemes.None) { return(ErrorCodes.NoResponse); } if (credentials.MessageQop.Equals(auth) == false) { return(ErrorCodes.QopNotSupported); } var epid = reader.From.Epid; if (epid.IsInvalid) { return(ErrorCodes.EpidNotFound); } if (credentials.HasGssapiData) { if (credentials.Version != 3) { return(ErrorCodes.VersionNotSupported); } var sa = connectingAssociations.GetOrAdd(opaque, scheme, GetDomain(credentials.Targetname), SecurityAssociationFactory1); var result = sa.Authentication(credHandle, Convert.FromBase64String(credentials.GssapiData.IsValid ? credentials.GssapiData.ToString() : String.Empty), out token); if (result != ErrorCodes.Continue) { connectingAssociations.Remove(opaque); if (result == ErrorCodes.Ok) { if (sa.UserName.Equals(reader.From.AddrSpec.User.ToString(), StringComparison.OrdinalIgnoreCase) == false) { result = ErrorCodes.UsernameNotMatch; } else { if (IsAuthorized(GetDomain(credentials.Targetname), sa.UserName.ToLower()) == false) { result = ErrorCodes.NotAuthorized; } else { var old = authorizedAssociations.Replace(epid.ToString(), sa); if (old != null) { old.Dispose(); } } } } if (result != ErrorCodes.Ok) { sa.Dispose(); } } return(result); } else { // verify signature // SecurityAssociation sa; if (authorizedAssociations.TryGetValue(epid.ToString(), out sa) == false) { return(ErrorCodes.NotAuthenticated); } if (credentials.Crand == int.MinValue) { return(ErrorCodes.CrandRequired); } if (credentials.Cnum == int.MinValue) { return(ErrorCodes.CnumRequired); } if (credentials.HasResponse == false) { return(ErrorCodes.ResponseRequired); } if (credentials.Opaque.IsValid && opaque != sa.Opaque) { return(ErrorCodes.UnexpectedOpaque); } if (sa.VerifySignature(reader, credentials) == false) { return(ErrorCodes.InvalidSignature); } if (sa.UserName.Equals(reader.From.AddrSpec.User.ToString(), StringComparison.OrdinalIgnoreCase) == false) { return(ErrorCodes.UsernameNotMatch); } return(ErrorCodes.Ok); } }