Skip to content

Commit c039433

Browse files
authored
Merge pull request #63 from fossapps/federation
Federation
2 parents c330738 + ec5cef7 commit c039433

34 files changed

+217
-494
lines changed

Micro.Auth.Api/Authentication/Exceptions/BadBasicAuthorizationDataException.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

Micro.Auth.Api/Authentication/SessionController.cs

Lines changed: 0 additions & 150 deletions
This file was deleted.

Micro.Auth.Api/Authentication/ViewModels/JwtRefreshViewModels.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

Micro.Auth.Api/GraphQL/AuthSchema.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2-
using Micro.Auth.Api.GraphQL.Federation;
2+
using Micro.Auth.Api.GraphQL.Types;
3+
using Micro.GraphQL.Federation;
34
using Micro.Auth.Api.GraphQL.Directives;
45

56
namespace Micro.Auth.Api.GraphQL
67
{
7-
public class AuthSchema : Schema
8+
public class AuthSchema : Schema<EntityType>
89
{
910
public AuthSchema(IServiceProvider services, Query query, Mutation mutation) : base(services)
1011
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Micro.Auth.Api.GraphQL.Extensions.Exceptions
4+
{
5+
public class InvalidTokenTypeException : Exception
6+
{
7+
public InvalidTokenTypeException(string expectedType) : base($"{expectedType} token not found")
8+
{
9+
}
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Micro.Auth.Api.GraphQL.Extensions.Exceptions
4+
{
5+
public class MissingHeaderException : Exception
6+
{
7+
public MissingHeaderException(string headerName) : base($"Missing {headerName} header")
8+
{
9+
}
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Micro.Auth.Api.Internal.UserData.Extensions;
2+
using Micro.Auth.Business.Sessions;
3+
using Microsoft.AspNetCore.Http;
4+
5+
namespace Micro.Auth.Api.GraphQL.Extensions
6+
{
7+
public static class LoginRequestExtension
8+
{
9+
public static LoginRequest GetLoginRequest(this IHttpContextAccessor httpContextAccessor)
10+
{
11+
var (login, password) = httpContextAccessor.MustGetBasicToken();
12+
return new LoginRequest
13+
{
14+
Login = login,
15+
Password = password,
16+
IpAddress = httpContextAccessor.GetIpAddress(),
17+
UserAgent = httpContextAccessor.GetUserAgent(),
18+
Location = httpContextAccessor.GetRoughLocation()
19+
};
20+
}
21+
}
22+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Text;
3+
using Micro.Auth.Api.GraphQL.Extensions.Exceptions;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.Extensions.Primitives;
6+
7+
namespace Micro.Auth.Api.GraphQL.Extensions
8+
{
9+
public static class Tokens
10+
{
11+
public static string MustGetBearerToken(this IHttpContextAccessor httpContextAccessor)
12+
{
13+
var headerValue = httpContextAccessor.MustGetAuthHeader();
14+
if (!headerValue.StartsWith("Bearer "))
15+
{
16+
throw new InvalidTokenTypeException("Bearer");
17+
}
18+
return headerValue.Substring("Bearer ".Length).Trim();
19+
}
20+
21+
private static string MustGetAuthHeader(this IHttpContextAccessor httpContextAccessor)
22+
{
23+
var header = StringValues.Empty;
24+
var exists = httpContextAccessor.HttpContext?.Request.Headers.TryGetValue("Authorization", out header);
25+
if (exists == false || header.ToString() == "")
26+
{
27+
throw new MissingHeaderException("Authorization");
28+
}
29+
30+
return header.ToString();
31+
}
32+
33+
public static (string, string) MustGetBasicToken(this IHttpContextAccessor httpContextAccessor)
34+
{
35+
try
36+
{
37+
var headerValue = httpContextAccessor.MustGetAuthHeader();
38+
if (!headerValue.StartsWith("Basic "))
39+
{
40+
throw new InvalidTokenTypeException("Basic");
41+
}
42+
43+
var token = headerValue.Substring("Basic ".Length).Trim();
44+
var parts = Encoding.UTF8.GetString(Convert.FromBase64String(token)).Split(":");
45+
return (parts[0], parts[1]);
46+
}
47+
catch (Exception e)
48+
{
49+
throw new InvalidTokenTypeException(e.Message);
50+
}
51+
}
52+
}
53+
}

Micro.Auth.Api/GraphQL/Federation/EntityType.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Micro.Auth.Api/GraphQL/Federation/ExtendsDirective.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)