Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifiy Message Extension #7

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion NP.Lti13Platform.Core/Configs/Lti13PlatformCoreConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public class Lti13PlatformTokenConfig
{
private const string INVALID_ISSUER = "Issuer must follow the guidelines in the LTI 1.3 security spec. https://www.imsglobal.org/spec/security/v1p0/#dfn-issuer-identifier";
private const string INVALID_TOKEN_AUDIENCE = "Token Audience must follow the guidelines in the LTI 1.3 security spec. https://www.imsglobal.org/spec/security/v1p0/#dfn-issuer-identifier";

private string _issuer = string.Empty;
/// <summary>
Expand Down
42 changes: 12 additions & 30 deletions NP.Lti13Platform.Core/Lti13PlatformBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,18 @@ public Lti13PlatformBuilder ExtendLti13Message<T, U>(string? messageType = null)
{
if (!MessageTypes.TryGetValue(messageType, out var mt))
{
AddMessageHandler(messageType);
MessageTypes.TryAdd(messageType, new MessageType(messageType, [.. GlobalInterfaces]));

foreach (var globalPopulator in GlobalPopulators)
{
services.AddKeyedTransient(typeof(Populator), messageType, globalPopulator);
}

services.AddKeyedTransient(messageType, (sp, obj) =>
{
return (LtiMessage)Activator.CreateInstance(LtiMessageTypes[messageType])!;
});

mt = MessageTypes[messageType];
}

Expand All @@ -61,23 +72,6 @@ public Lti13PlatformBuilder ExtendLti13Message<T, U>(string? messageType = null)
return this;
}

public Lti13PlatformMessageBuilder AddMessageHandler(string messageType)
{
MessageTypes.TryAdd(messageType, new MessageType(messageType, [.. GlobalInterfaces]));

foreach (var globalPopulator in GlobalPopulators)
{
services.AddKeyedTransient(typeof(Populator), messageType, globalPopulator);
}

services.AddKeyedTransient(messageType, (sp, obj) =>
{
return (LtiMessage)Activator.CreateInstance(LtiMessageTypes[messageType])!;
});

return new Lti13PlatformMessageBuilder(services, messageType);
}

internal static void CreateTypes()
{
if (LtiMessageTypes.Count == 0)
Expand All @@ -93,16 +87,4 @@ internal static void CreateTypes()

public IServiceCollection Services => services;
}

public class Lti13PlatformMessageBuilder(IServiceCollection baseCollection, string messageType) : Lti13PlatformBuilder(baseCollection)
{
public Lti13PlatformMessageBuilder Extend<T, U>()
where T : class
where U : Populator<T>
{
base.ExtendLti13Message<T, U>(messageType);

return this;
}
}
}
3 changes: 1 addition & 2 deletions NP.Lti13Platform.Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ builder.Services

## Custom Messgage Types

LTI allows for message types not defined officially in any spec. A custom message type can be defined between tools and platforms. This is handled here by creating a new message handler and extending it with the interfaces and populators it needs.
LTI allows for message types not defined officially in any spec. A custom message type can be defined between tools and platforms. This is handled here by extending a message type name it with the interfaces and populators it needs.

```csharp
interface ICustomMessage
Expand All @@ -223,7 +223,6 @@ class CustomPopulator: Populator<ICustomMessage>

builder.Services
.AddLti13PlatformCore()
.AddMessageHandler("CustomMessage")
.ExtendLti13Message<ICustomMessage, CustomPopulator>("CustomMessage");
```

Expand Down
12 changes: 6 additions & 6 deletions NP.Lti13Platform.Core/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public static Lti13PlatformBuilder AddLti13PlatformCore(this IServiceCollection

builder.Services.AddTransient<IUrlServiceHelper, UrlServiceHelper>();

builder.AddMessageHandler(Lti13MessageType.LtiResourceLinkRequest)
.Extend<IResourceLinkMessage, ResourceLinkPopulator>()
.Extend<IPlatformMessage, PlatformPopulator>()
.Extend<IContextMessage, ContextPopulator>()
.Extend<ICustomMessage, CustomPopulator>()
.Extend<IRolesMessage, RolesPopulator>();
builder
.ExtendLti13Message<IResourceLinkMessage, ResourceLinkPopulator>(Lti13MessageType.LtiResourceLinkRequest)
.ExtendLti13Message<IPlatformMessage, PlatformPopulator>(Lti13MessageType.LtiResourceLinkRequest)
.ExtendLti13Message<IContextMessage, ContextPopulator>(Lti13MessageType.LtiResourceLinkRequest)
.ExtendLti13Message<ICustomMessage, CustomPopulator>(Lti13MessageType.LtiResourceLinkRequest)
.ExtendLti13Message<IRolesMessage, RolesPopulator>(Lti13MessageType.LtiResourceLinkRequest);

builder.Services.AddAuthentication()
.AddScheme<AuthenticationSchemeOptions, LtiServicesAuthHandler>(LtiServicesAuthHandler.SchemeName, null);
Expand Down
12 changes: 6 additions & 6 deletions NP.Lti13Platform.DeepLinking/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public static class Startup
{
public static Lti13PlatformBuilder AddLti13PlatformDeepLinking(this Lti13PlatformBuilder builder)
{
builder.AddMessageHandler(Lti13MessageType.LtiDeepLinkingRequest)
.Extend<IDeepLinkingMessage, DeepLinkingPopulator>()
.Extend<IPlatformMessage, PlatformPopulator>()
.Extend<IContextMessage, ContextPopulator>()
.Extend<ICustomMessage, CustomPopulator>()
.Extend<IRolesMessage, RolesPopulator>();
builder
.ExtendLti13Message<IDeepLinkingMessage, DeepLinkingPopulator>(Lti13MessageType.LtiDeepLinkingRequest)
.ExtendLti13Message<IPlatformMessage, PlatformPopulator>(Lti13MessageType.LtiDeepLinkingRequest)
.ExtendLti13Message<IContextMessage, ContextPopulator>(Lti13MessageType.LtiDeepLinkingRequest)
.ExtendLti13Message<ICustomMessage, CustomPopulator>(Lti13MessageType.LtiDeepLinkingRequest)
.ExtendLti13Message<IRolesMessage, RolesPopulator>(Lti13MessageType.LtiDeepLinkingRequest);

return builder;
}
Expand Down