Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. import { Aurelia } from 'aurelia-framework';
  2. import 'src/helpers/exceptionHelpers'
  3. import config from "./auth-config";
  4.  
  5. export function configure(aurelia: Aurelia) {
  6. aurelia.use
  7. .standardConfiguration()
  8. .feature('src/resources')
  9. .developmentLogging()
  10. .plugin('aurelia-dialog')
  11. .plugin('aurelia-api', config => {
  12. // Register an authentication hosts
  13. config.registerEndpoint('auth', 'http://localhost:7987/auth/');
  14. })
  15. .plugin('aurelia-authentication', (baseConfig) => {
  16. baseConfig.configure(config);
  17. });
  18.  
  19. aurelia.start().then(x => x.setRoot('src/app'));
  20. }
  21.  
  22. var config = {
  23. endpoint: 'auth', // use 'auth' endpoint for the auth server
  24. configureEndpoints: ['auth'], // add Authorization header to 'auth' endpoint
  25.  
  26. // The API specifies that new users register at the POST /users enpoint
  27. signupUrl: null,
  28. // The API endpoint used in profile requests (inc. `find/get` and `update`)
  29. profileUrl: null,
  30. // Logins happen at the POST /sessions/create endpoint
  31. loginUrl: '',
  32. // The API serves its tokens with a key of id_token which differs from
  33. // aurelia-auth's standard
  34. accessTokenName: 'SessionId',
  35. // Once logged in, we want to redirect the user to the welcome view
  36. loginRedirect: '#/pending',
  37. // The SPA url to which the user is redirected after a successful logout
  38. logoutRedirect: '#/login',
  39. // The SPA route used when an unauthenticated user tries to access an SPA page that requires authentication
  40. loginRoute : '#/help'
  41. };
  42.  
  43. export default config;
  44.  
  45. import { AuthService } from 'aurelia-authentication';
  46. import { inject, computedFrom } from 'aurelia-framework';
  47.  
  48. @inject(AuthService)
  49. export class Login {
  50. heading: string;
  51. auth: AuthService;
  52. userName: string;
  53. password: string;
  54.  
  55. constructor(authService) {
  56. this.auth = authService;
  57. this.heading = 'Login';
  58. }
  59.  
  60. login() {
  61. var credentials = {
  62. username: this.userName,
  63. password: this.password,
  64. grant_type: "password"
  65. };
  66. return this.auth.login(credentials,
  67. { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }
  68. ).then(response => {
  69. console.log("success logged " + response);
  70. })
  71. .catch(err => {
  72. console.log("login failure");
  73. });
  74. };
  75. }
  76.  
  77. public override void Configure(Container container)
  78. {
  79. var privateKey = RsaUtils.CreatePrivateKeyParams(RsaKeyLengths.Bit2048);
  80. var publicKey = privateKey.ToPublicRsaParameters();
  81. var privateKeyXml = privateKey.ToPrivateKeyXml();
  82. var publicKeyXml = privateKey.ToPublicKeyXml();
  83.  
  84. SetConfig(new HostConfig
  85. {
  86. #if DEBUG
  87. DebugMode = true,
  88. WebHostPhysicalPath = Path.GetFullPath(Path.Combine("~".MapServerPath(), "..", "..")),
  89. #endif
  90. });
  91. container.RegisterAs<LDAPAuthProvider, IAuthProvider>();
  92. container.Register<ICacheClient>(new MemoryCacheClient { FlushOnDispose = false });
  93. container.RegisterAs<MemoryCacheClient, ICacheClient>();
  94. Plugins.Add(new AuthFeature(() => new AuthUserSession(),
  95. new[] {
  96. container.Resolve<IAuthProvider>(),
  97. new JwtAuthProvider {
  98. HashAlgorithm = "RS256",
  99. PrivateKeyXml = privateKeyXml,
  100. RequireSecureConnection = false,
  101. }
  102. })
  103. {
  104. HtmlRedirect = "~/#/pending",
  105. IncludeRegistrationService = false,
  106. IncludeAssignRoleServices = false,
  107. MaxLoginAttempts = Settings.Default.MaxLoginAttempts
  108. });
  109. }
  110.  
  111. public class LDAPAuthProvider : CredentialsAuthProvider
  112. {
  113. private readonly IHoldingsManagerSettings _settings;
  114.  
  115. public LDAPAuthProvider(IHoldingsManagerSettings settings)
  116. {
  117. _settings = settings;
  118. }
  119. public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
  120. {
  121. //Check to see if the username/password combo is valid, an exception will be thrown if the username or password is wrong
  122. try
  123. {
  124. var entry = new DirectoryEntry($"LDAP://{_settings.Domain}", userName, password);
  125. var nativeObject = entry.NativeObject;
  126. using (var identity = new WindowsIdentity(userName))
  127. {
  128. var principal = new WindowsPrincipal(identity);
  129. return principal.IsInRole(_settings.AdminGroupName);
  130. }
  131. }
  132. catch (Exception)
  133. {
  134. //This means the username/password combo failed
  135. return false;
  136. }
  137. }
  138.  
  139. public override IHttpResult OnAuthenticated(IServiceBase authService,
  140. IAuthSession session,
  141. IAuthTokens tokens,
  142. Dictionary<string, string> authInfo)
  143. {
  144. //Fill IAuthSession with data you want to retrieve in the app eg:
  145. session.DisplayName = "Testy McTesterson";
  146. //...
  147.  
  148. //Call base method to Save Session and fire Auth/Session callbacks:
  149. return base.OnAuthenticated(authService, session, tokens, authInfo);
  150.  
  151. //Alternatively avoid built-in behavior and explicitly save session with
  152. //authService.SaveSession(session, SessionExpiry);
  153. //return null;
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement