使用しているAPIGatewayエンドポイントは、特定のオーディエンスへのアクセス許可によって制限されます。
ラムダオーソライザーを使用して外部サービスからアクセス許可を取得し、エンドポイントへのアクセスを許可または拒否するポリシーを作成するという考え方です。
APIエンドポイントにアクセス許可を一致させるには、エンドポイントが必要なアクセス許可を承認者に提供する必要があります。
私の質問は、エンドポイントデータを独自の必要なアクセス許可で強化し、それらをオーソライザーラムダで(おそらくイベントを介して)使用してさらに検証する方法です。
例:
これがラムダの私のコードです:
import {Callback, Context} from 'aws-lambda';
import {Authorizer} from './authorizer';
export class App {
constructor(private authorizer: Authorizer = new Authorizer()) {
}
public handleEvent(event, callback: Callback): Promise<void> {
return this.authorizer.checkAuthorization(event, callback)
.then((policy) => callback(null, policy))
.catch((error) => callback(error, null));
}
}
const app: App = new App();
module.exports.lambda_handler = async (event) => {
return await app.handleEvent(event);
};
checkAuthorizationメソッドのコード:
export class Authorizer {
public resourceAuthorizer: ResourceAuthorizer = new ResourceAuthorizer();
public authenticationChecker: AuthenticationChecker = new AuthenticationChecker();
public checkAuthorization(event, callback): Promise<object> {
const endpointPermissions = event.endpointPermissions; // <== this is what I need, a custom field in the event which
// is provided from the api endpoint in some way
// in my example this whould contain a string or json
// with 'View:Pets' and 'View:Somethingelse'
return this.authenticationChecker.check(event)
.then((decodedJwt) => {
const principalId: string = decodedJwt.payload.sub;
return Promise.resolve(decodedJwt)
.then((jwt) => this.resourceAuthorizer.check(jwt, event.endpointPermissions))
.then((payload) => callback(null,
getAuthorizationPolicy(principalId, 'Allow', event.endpointPermissions, payload)))
.catch((payload) => callback(null,
getAuthorizationPolicy(principalId, 'Deny', event.endpointPermissions, payload)));
}).catch((error) => {
console.log(error);
callback('Unauthorized');
});
}
}
event.endpointPermissions私が探しています何を基本的にあります。APIエンドポイントに応じて、そのエンドポイントに必要な権限を入力する必要があります。次に、resourceAuthorizerは、外部サービスからユーザーのアクセス許可を取得し、それらをendpointPermissionsと比較して、許可または拒否ポリシーを作成します。
では、APIエンドポイントのendpointPermissionsをどこに入力して、承認者に提供できますか?
Authorizerに渡されるイベントには、次の形式のmethodArnが含まれています。
arn:aws:execute-api:<Region id>:<Account id>:<API id>/<Stage>/<Method>/<Resource path>
これにより、必要なメソッドとリソースパスが得られます。また、APIの識別子は提供されますが、API自体の名前は提供されません。
API IDは、AWSSDKを使用してAPI名を取得するために使用できます。こちらをご覧ください。
これにより、endpointPermissions値を作成するために必要なすべてのものが得られます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加