0

I have issues authentication myself in my application. When I try to access I get 401 access denied, although the token is successfully granted.

My azure-ad.guard.ts:

import { Injectable, Logger } from "@nestjs/common";
import { PassportStrategy, AuthGuard } from "@nestjs/passport";
import {BearerStrategy} from "passport-azure-ad";

const clientID = 'my application guid';
const tenantID = 'my tenant guid'; // xxx Tenant

/**
 * Extracts ID token from header and validates it.
 */
@Injectable()
export class AzureAdStrategy extends PassportStrategy(
    BearerStrategy,
    'azure-ad',
){
constructor() {
    super({
        identityMetadata: `https://login.microsoftonline.com/${tenantID}/v2.0/.well-known/openid-configuration`,
        clientID,
        });
    }

    async validate(data) {
        Logger.verbose(data);
        return data;
    }
}

export const AzureADGuard = AuthGuard('azure-ad');

And here my shortened controller - contacts.controller.ts

import { Body, Controller, Get, Param, Patch, Post, Logger, HttpStatus, UseGuards} from         '@nestjs/common';
import { ContactsService } from './contacts.service';
import { CreateContactDto } from './dto/create-contact.dto';
import { Contact } from './schema/contact.schema';
import { AzureADGuard } from 'src/azure-ad.guard';

@Controller('contacts')
export class ContactsController {
constructor(private readonly contactsService: ContactsService) {}


@Get(':funechID')
@UseGuards(AzureADGuard)
async getContact(@Param('funechID') funechID: number): Promise<Contact> {
    try {
        return this.contactsService.getContactById(funechID);
    } catch (error) {
       Logger.error(error,"","Class:"+ContactsController.name)
       }
    }
// other methods like @Get ... 
}

} How can I troubleshoot this, as you could see I also tried to give me an output from the azure-ad.guard.ts but this isn't called. Did I miss something?

I used this article to get my code running... Medium.com - AzureAD authentication for NestJS

XRJPK
  • 63
  • 1
  • 8

0 Answers0