mirror of
https://github.com/bitwarden/server
synced 2026-01-10 04:23:31 +00:00
stub out new scim api for dir sync
This commit is contained in:
34
src/Scim/Models/ScimError.cs
Normal file
34
src/Scim/Models/ScimError.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Scim.Models
|
||||
{
|
||||
public class ScimError
|
||||
{
|
||||
private IEnumerable<string> _schemas;
|
||||
|
||||
public ScimError()
|
||||
{
|
||||
_schemas = new[] { Constants.Messages.Error };
|
||||
}
|
||||
|
||||
public ScimError(HttpStatusCode status, string detail = null)
|
||||
: this()
|
||||
{
|
||||
Status = (int)status;
|
||||
Detail = detail;
|
||||
}
|
||||
|
||||
[JsonProperty("schemas")]
|
||||
public IEnumerable<string> Schemas
|
||||
{
|
||||
get => _schemas;
|
||||
set { _schemas = value; }
|
||||
}
|
||||
[JsonProperty("status")]
|
||||
public int Status { get; set; }
|
||||
[JsonProperty("detail")]
|
||||
public string Detail { get; set; }
|
||||
}
|
||||
}
|
||||
42
src/Scim/Models/ScimGroup.cs
Normal file
42
src/Scim/Models/ScimGroup.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core.Models.Table;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Scim.Models
|
||||
{
|
||||
public class ScimGroup : ScimResource
|
||||
{
|
||||
public ScimGroup() { }
|
||||
|
||||
public ScimGroup(Group group)
|
||||
{
|
||||
Id = group.Id.ToString();
|
||||
ExternalId = group.ExternalId;
|
||||
DisplayName = group.Name;
|
||||
Meta = new ScimResourceMetadata("Group");
|
||||
}
|
||||
|
||||
public override string SchemaIdentifier => Constants.Schemas.Group;
|
||||
[JsonProperty("displayName")]
|
||||
public string DisplayName { get; set; }
|
||||
[JsonProperty("members")]
|
||||
public IEnumerable<ScimMultiValuedAttribute> Members { get; set; }
|
||||
|
||||
public Group ToGroup(Guid orgId)
|
||||
{
|
||||
return new Group
|
||||
{
|
||||
ExternalId = ExternalId,
|
||||
Name = DisplayName,
|
||||
OrganizationId = orgId
|
||||
};
|
||||
}
|
||||
|
||||
public Group ToGroup(Group group)
|
||||
{
|
||||
group.Name = DisplayName;
|
||||
return group;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/Scim/Models/ScimListResponse.cs
Normal file
24
src/Scim/Models/ScimListResponse.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Scim.Models
|
||||
{
|
||||
public class ScimListResponse : ScimSchemaBase
|
||||
{
|
||||
public ScimListResponse(IEnumerable<ScimResource> resources)
|
||||
{
|
||||
Resources = resources;
|
||||
}
|
||||
|
||||
public override string SchemaIdentifier => Constants.Messages.ListResponse;
|
||||
[JsonProperty("totalResults", Order = 0)]
|
||||
public int TotalResults => Resources == null ? 0 : Resources.Count();
|
||||
[JsonProperty("Resources", Order = 1)]
|
||||
public IEnumerable<ScimResource> Resources { get; private set; }
|
||||
[JsonProperty("startIndex", Order = 2)]
|
||||
public int StartIndex { get; set; } = 0;
|
||||
[JsonProperty("itemsPerPage", Order = 3)]
|
||||
public int ItemsPerPage => Resources == null ? 0 : Resources.Count();
|
||||
}
|
||||
}
|
||||
19
src/Scim/Models/ScimMultiValuedAttribute.cs
Normal file
19
src/Scim/Models/ScimMultiValuedAttribute.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Scim.Models
|
||||
{
|
||||
public class ScimMultiValuedAttribute
|
||||
{
|
||||
[JsonProperty("type")]
|
||||
public string Type { get; set; }
|
||||
[JsonProperty("primary")]
|
||||
public bool Primary { get; set; }
|
||||
[JsonProperty("display")]
|
||||
public string Display { get; set; }
|
||||
[JsonProperty("value")]
|
||||
public string Value { get; set; }
|
||||
[JsonProperty("$ref")]
|
||||
public Uri Ref { get; set; }
|
||||
}
|
||||
}
|
||||
14
src/Scim/Models/ScimResource.cs
Normal file
14
src/Scim/Models/ScimResource.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Scim.Models
|
||||
{
|
||||
public abstract class ScimResource : ScimSchemaBase
|
||||
{
|
||||
[JsonProperty(Order = -5, PropertyName = "id")]
|
||||
public string Id { get; set; }
|
||||
[JsonProperty(PropertyName = "externalId")]
|
||||
public string ExternalId { get; set; }
|
||||
[JsonProperty(Order = 9999, PropertyName = "meta")]
|
||||
public ScimResourceMetadata Meta { get; set; }
|
||||
}
|
||||
}
|
||||
17
src/Scim/Models/ScimResourceMetadata.cs
Normal file
17
src/Scim/Models/ScimResourceMetadata.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Scim.Models
|
||||
{
|
||||
public class ScimResourceMetadata
|
||||
{
|
||||
private ScimResourceMetadata() { }
|
||||
|
||||
public ScimResourceMetadata(string resourceType)
|
||||
{
|
||||
ResourceType = resourceType;
|
||||
}
|
||||
|
||||
[JsonProperty("resourceType")]
|
||||
public string ResourceType { get; set; }
|
||||
}
|
||||
}
|
||||
13
src/Scim/Models/ScimSchemaBase.cs
Normal file
13
src/Scim/Models/ScimSchemaBase.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Scim.Models
|
||||
{
|
||||
public abstract class ScimSchemaBase
|
||||
{
|
||||
[JsonProperty("schemas", Order = -10)]
|
||||
public virtual ISet<string> Schemas => new HashSet<string>(new[] { SchemaIdentifier });
|
||||
[JsonIgnore]
|
||||
public abstract string SchemaIdentifier { get; }
|
||||
}
|
||||
}
|
||||
63
src/Scim/Models/ScimUser.cs
Normal file
63
src/Scim/Models/ScimUser.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Core.Models.Table;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Scim.Models
|
||||
{
|
||||
public class ScimUser : ScimResource
|
||||
{
|
||||
public ScimUser() { }
|
||||
|
||||
public ScimUser(OrganizationUserUserDetails userDetails)
|
||||
{
|
||||
Id = userDetails.Id.ToString();
|
||||
ExternalId = userDetails.ExternalId;
|
||||
UserName = userDetails.Email;
|
||||
Name = new ScimName
|
||||
{
|
||||
Formatted = userDetails.Name
|
||||
};
|
||||
DisplayName = userDetails.Name;
|
||||
Active = true;
|
||||
Emails = new List<ScimMultiValuedAttribute> {
|
||||
new ScimMultiValuedAttribute { Type = "work", Value = userDetails.Email } };
|
||||
Meta = new ScimResourceMetadata("User");
|
||||
}
|
||||
|
||||
public ScimUser(OrganizationUser orgUser)
|
||||
{
|
||||
Id = orgUser.Id.ToString();
|
||||
ExternalId = orgUser.ExternalId;
|
||||
UserName = orgUser.Email;
|
||||
Active = true;
|
||||
Emails = new List<ScimMultiValuedAttribute> {
|
||||
new ScimMultiValuedAttribute { Type = "work", Value = orgUser.Email } };
|
||||
Meta = new ScimResourceMetadata("User");
|
||||
}
|
||||
|
||||
public override string SchemaIdentifier => Constants.Schemas.User;
|
||||
[JsonProperty("userName")]
|
||||
public string UserName { get; set; }
|
||||
[JsonProperty("name")]
|
||||
public ScimName Name { get; set; }
|
||||
[JsonProperty("displayName")]
|
||||
public string DisplayName { get; set; }
|
||||
[JsonProperty("active")]
|
||||
public bool Active { get; set; }
|
||||
[JsonProperty("emails")]
|
||||
public IEnumerable<ScimMultiValuedAttribute> Emails { get; set; }
|
||||
[JsonProperty("groups")]
|
||||
public IEnumerable<ScimMultiValuedAttribute> Groups { get; set; }
|
||||
|
||||
public class ScimName
|
||||
{
|
||||
[JsonProperty("formatted")]
|
||||
public string Formatted { get; set; }
|
||||
[JsonProperty("familyName")]
|
||||
public string FamilyName { get; set; }
|
||||
[JsonProperty("givenName")]
|
||||
public string GivenName { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user