本文整理汇总了TypeScript中passport-jwt.ExtractJwt类的典型用法代码示例。如果您正苦于以下问题:TypeScript ExtractJwt类的具体用法?TypeScript ExtractJwt怎么用?TypeScript ExtractJwt使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ExtractJwt类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: before
before((done) => {
const app = express();
passport.use(new JwtStrategy({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: JWT_SECRET
}, (payload: any, done: VerifiedCallback) => {
done(null, payload.name);
}));
app.use(passport.initialize());
app.use(passport.authenticate('jwt', { session: false }));
app.get('/', (req, res) => {
res.send('hello world! ' + req.user);
});
server = app.listen(3000, () => {
done();
});
client = axios.create({
baseURL: 'http://localhost:3000',
validateStatus: status => true
});
});
示例2: constructor
constructor(private readonly authService: AuthService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'secretKey',
algorithms: 'HS512'
});
}
示例3: config
config() {
let opts = {
secretOrKey: config.secret,
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme('bearer')
};
passport.use(new Strategy(opts, (jwtPayload, done) => {
User
.getById(jwtPayload.id)
.then(user => {
if (user) {
return done(null, {
id: user.id,
email: user.email
});
}
return done(null, false);
})
.catch(error => {
done(error, null);
})
}));
return {
initialize: () => passport.initialize(),
authenticate: () => passport.authenticate('jwt', { session: false })
}
}
示例4: constructor
constructor(private readonly authService: AuthService,
private readonly config: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.get('JWT_KEY'),
});
}
示例5: constructor
constructor(private readonly authService: AuthService) {
super(
{
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
passReqToCallback: true,
secretOrKey: process.env.JWT_SECRET,
},
async (req, payload, next) => await this.verify(req, payload, next)
);
passport.use(this);
}
示例6: constructor
constructor(private readonly authService: AuthService) {
super({
//用來帶入驗證的函式
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
//設成true就可以在verify的callback中使用
passReqToCallback: true,
secretOrKey: 'donttalk',
},
async (req, payload, next) => await this.verify(req, payload, next)
);
passport.use(this);
}
示例7: function
function (model: DatabaseObject, passport: Passport): void {
let opts: any = {}
opts.secretOrKey = model.tokenSalt
opts.jwtFromRequest = ExtractJwt.fromHeader('token')
let strategy = new Strategy(opts, (jwtPayload, done) => {
model.user.findOne({ _id: jwtPayload._doc._id }).exec()
.then((account) => {
account === undefined ? done(undefined, false) : done(undefined, account)
}, (err) => {
done(err, false)
})
})
passport.use(strategy)
}
示例8: init
static init():void {
var JwtStrategy = require('passport-jwt').Strategy,
ExtractJwt = require('passport-jwt').ExtractJwt;
var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
opts.secretOrKey = DBConfig.secret;
passport.use(new JwtStrategy(opts, function(jwt_payload, done) {
User.findOne({id: jwt_payload.sub}, function(err, user) {
if (err) {
return done(err, false);
}
if (user) {
done(null, user);
} else {
done(null, false);
// or you could create a new account
}
});
}));
}
示例9: setupJwt
function setupJwt(kind: string): passport.Strategy {
const JWTStrategy = passportJWT.Strategy;
const ExtractJwt = require("passport-jwt").ExtractJwt;
var opts = {
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: config.get(`security.secrets.${kind.toUpperCase()}`),
passReqToCallback: true
};
return new JWTStrategy(opts, async (req: any, jwt_payload: IPayload, next: any) => {
logger.debug("payload received", jwt_payload);
// usually this would be a database call:
const user: IUser = await UserDB.getByToken(kind, jwt_payload);
if (user) {
(user as any).usedStrategy = kind;
next(null, user);
} else {
next(null, false);
}
});
}
示例10: done
if (equal) {
return done(null, user);
} else {
return done(err, false, { message: 'Failed to authenticate.' });
}
});
} else {
return done(err, false, { message: 'Failed to authenticate.' });
}
});
});
passport.use(strategy);
let jwtOptions: jwt.StrategyOptions = {
secretOrKey: 'qwerty',
jwtFromRequest: jwt.ExtractJwt.fromAuthHeaderWithScheme('jwt')
};
let jwtStrategy = new jwt.Strategy(
jwtOptions,
(payload, done) => {
let db = new UserRepository();
db.findById(payload.userId, (err, user) => {
if (err) {
done(err, false);
} else {
if (user) {
done(null, user);
} else {
done(null, false);
}
}