1
0
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:
Kyle Spearrin
2017-12-12 13:21:15 -05:00
parent 3d05c9208f
commit 7b359053d6
26 changed files with 802 additions and 14 deletions

View 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; }
}
}

View 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;
}
}
}

View 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();
}
}

View 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; }
}
}

View 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; }
}
}

View 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; }
}
}

View 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; }
}
}

View 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; }
}
}
}