首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何配置OAuth代码流清除发现

如何配置OAuth代码流清除发现
EN

Stack Overflow用户
提问于 2022-02-09 05:42:25
回答 2查看 1.1K关注 0票数 0

这是我第一次尝试使用OAuth。我正在使用角13 + 角-OAuth2-oidc库,并试图配置代码流。我的问题是,在教程中,发现文档是默认使用的,但是我的auth服务器没有这样的功能,所以在这个请求identity_provider_url/.well-known/openid-configuration的应用程序中,我得到了一个404错误。问题是如何配置代码流而不从auth服务器加载发现文档?我只为隐式流找到了没有发现文档的配置。这就是我的代码现在的样子:

auth.config.ts

代码语言:javascript
复制
import { AuthConfig } from 'angular-oauth2-oidc';

export const authCodeFlowConfig: AuthConfig = {
  // Url of the Identity Provider
  issuer: '**************', // identity provider URL,
  redirectUri: window.location.origin + '/index.html',
  clientId: 'spa',
  dummyClientSecret: 'secret',
  responseType: 'code',
  scope: 'read, write',
  showDebugInformation: true,
};

app.component.ts

代码语言:javascript
复制
import {Component} from '@angular/core';
import {authCodeFlowConfig} from './sso.confiig';
import {OAuthService} from 'angular-oauth2-oidc';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'sso';

  constructor(private oAuthService: OAuthService) {
    this.oAuthService.configure(authCodeFlowConfig);
    this.oauthService.loadDiscoveryDocumentAndTryLogin(); // I don`t want this
  }
}

login.component.ts

代码语言:javascript
复制
import {Component, OnInit} from '@angular/core';
import {OAuthErrorEvent, OAuthService} from 'angular-oauth2-oidc';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  constructor(private oAuthService: OAuthService) {
    this.oAuthService.events.subscribe(e => (e instanceof OAuthErrorEvent) ? console.error(e) : console.warn(e));
  }

  public async login(): Promise<void> {
    await this.oAuthService.initCodeFlow();
  }

  public logout(): void {
    this.oAuthService.logOut();
  }

  public get userName() {
    let claims = this.oAuthService.getIdentityClaims();
    if (!claims) return null;
    return claims;
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-10 20:03:57

查看AuthConfig类,尝试将loginUrl设置为授权端点,并显式设置令牌端点。看看这是否会给您带来不同的错误。

一个好的库应该允许您显式地设置端点。例如,几年前,我的SPA没有使用Azure AD,因为它不允许对令牌端点进行CORS请求。为此,我将oidc客户端库中的令牌端点设置为指向API,以便从那里调用令牌端点。

票数 2
EN

Stack Overflow用户

发布于 2022-02-11 12:25:08

扩展auth.config关于附加loginUrl和tokenUrl解决了这个问题。

这是我的配置文件的finall版本:

代码语言:javascript
复制
export const OAUTH_CONFIG: AuthConfig = {
  issuer: environment.identityProviderBaseUrl,
  loginUrl: environment.identityProviderLoginUrl,
  logoutUrl: environment.identityProviderLogoutUrl,
  redirectUri: environment.identityProvideRedirectUrl,
  tokenEndpoint: environment.identityProviderTokenUrl,
  clientId: environment.clientId,
  dummyClientSecret: environment.clientSecret,
  responseType: 'code',
  requireHttps: false,
  scope: 'write',
  showDebugInformation: true,
  oidc: false,
  useIdTokenHintForSilentRefresh: false
};

那么我们应该记住删除loadDiscoveryDocumentAndTryLogin()

代码语言:javascript
复制
  public configureCodeFlow(): void {
    this.authService.configure(OAUTH_CONFIG);
    this.authService.setupAutomaticSilentRefresh(undefined, 'access_token');
    this.authService.tryLoginCodeFlow();
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71044521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档